HttpPost的AsyncTask实现

时间:2014-05-23 17:37:01

标签: android android-asynctask client-server

我尝试使用此处提供的详细信息实现AsyncTask,但无济于事: How to fix android.os.NetworkOnMainThreadException?

按下按钮后应用程序崩溃(按钮调用PostServer()方法。我的程序只需要向服务器发送一个UDID。这是我的实现。

SecondAct.java:

public class SecondAct extends Activity {

public String getUDID(){
    String android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
    return android_id;
}
....
public void PostServer(View view){

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if(networkInfo != null && networkInfo.isConnected()){
        //send data
        new PostHttp().execute();
    }
    else{
        //display error
    }
}

PostHttp.java

public class PostHttp extends AsyncTask<Void,Integer,Void>  {
....
public static final String webphp = "http://...geo.php";
String id = (new SecondAct()).getUDID();

@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair(time,id));

    //Create the HTTP Post
    HttpParams httpParameters = new BasicHttpParams(); 

    //Setup timeouts -- for later

    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    HttpPost httppost = new HttpPost(webphp);
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;    
}
}

logcat的:

05-23 13:45:38.213: W/dalvikvm(2217): threadid=1: thread exiting with uncaught exception (group=0x41465700)
05-23 13:45:38.253: E/AndroidRuntime(2217): FATAL EXCEPTION: main
05-23 13:45:38.253: E/AndroidRuntime(2217): java.lang.IllegalStateException: Could not execute method of the activity
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.view.View$1.onClick(View.java:3633)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.view.View.performClick(View.java:4240)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.view.View$PerformClick.run(View.java:17721)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.os.Handler.handleCallback(Handler.java:730)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.os.Looper.loop(Looper.java:137)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at java.lang.reflect.Method.invoke(Method.java:525)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at dalvik.system.NativeStart.main(Native Method)
05-23 13:45:38.253: E/AndroidRuntime(2217): Caused by: java.lang.reflect.InvocationTargetException
05-23 13:45:38.253: E/AndroidRuntime(2217):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at java.lang.reflect.Method.invoke(Method.java:525)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at android.view.View$1.onClick(View.java:3628)
05-23 13:45:38.253: E/AndroidRuntime(2217):     ... 11 more
05-23 13:45:38.253: E/AndroidRuntime(2217): Caused by: java.lang.NullPointerException
05-23 13:45:38.253: E/AndroidRuntime(2217):     at com.example.webapp.SecondAct.getUDID(SecondAct.java:50)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at com.example.webapp.PostHttp.<init>(PostHttp.java:26)
05-23 13:45:38.253: E/AndroidRuntime(2217):     at com.example.webapp.SecondAct.PostServer(SecondAct.java:60)
05-23 13:45:38.253: E/AndroidRuntime(2217):     ... 14 more

0 个答案:

没有答案