-2

I'm trying to upload a png file to a server. What is the problem? I think there is a problem about connection but address is correct. It is a php URL. So, what is the problem? It does not upload image.

ByteArrayOutputStream stream = new ByteArrayOutputStream();
BMUpload.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair( "id", String.valueOf(((App)Global).m_iUserID) ));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(adrupload);
try
{
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    httpclient.execute(httppost);
}catch(Exception e)
{
    e.printStackTrace();
    return;
}

error log:

01-05 01:50:11.591: W/System.err(11415): android.os.NetworkOnMainThreadException
01-05 01:50:11.596: W/System.err(11415):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
01-05 01:50:11.596: W/System.err(11415):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-05 01:50:11.596: W/System.err(11415):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-05 01:50:11.596: W/System.err(11415):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-05 01:50:11.596: W/System.err(11415):    at com.todogram.Settings.UpdateAvatar(Settings.java:351)
01-05 01:50:11.596: W/System.err(11415):    at com.todogram.Settings.onActivityResult(Settings.java:139)
01-05 01:50:11.596: W/System.err(11415):    at android.app.Activity.dispatchActivityResult(Activity.java:5563)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.access$1200(ActivityThread.java:159)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-05 01:50:11.596: W/System.err(11415):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 01:50:11.596: W/System.err(11415):    at android.os.Looper.loop(Looper.java:137)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.main(ActivityThread.java:5419)
01-05 01:50:11.596: W/System.err(11415):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 01:50:11.596: W/System.err(11415):    at java.lang.reflect.Method.invoke(Method.java:525)
01-05 01:50:11.596: W/System.err(11415):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-05 01:50:11.596: W/System.err(11415):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-05 01:50:11.601: W/System.err(11415):    at dalvik.system.NativeStart.main(Native Method)
Melih Akalan
  • 57
  • 1
  • 1
  • 8

1 Answers1

0

Android OS prevents any application from using Network related processes (e.g. loading a site, sending or receiving data from a server, etc) to be executed in the main thread. The logic behind this is that network processes tend to be slow (more so if you have slow connection) and placing a process like this on the main thread will greatly slow the program down - and a slow program is a program with bad user experience.

You have to put it in a thread and use that thread in your onCreate() function or in a button pressed function.

This link should get you started with threads and file upload.

Razgriz
  • 7,179
  • 17
  • 78
  • 150