我创建了一个最简单的应用程序,一个默认的应用程序,我想尝试从php页面MyActivty.Java中发布和检索某些内容,如下所示:
package com.dolganiuc.victor.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class MainActivity extends ActionBarActivity {
private static final String TAG = "victorMessage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
postData();
}
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://93.117.158.187/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.i(TAG, response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
并且当Android Studio调试“所有工作正常”时,我在2个设备上运行了这些应用程序Android 4.4.2 KitKat和Android 5.0 Lollipop,并且在这两个设备上,该应用程序都将关闭以检索错误,不幸的是MyApplication已关闭。
问题是什么?
系统错误
06-11 13:18:02.042 1142-1161/? W/System.err﹕ java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneBase.privatizeCellInfoList(PhoneBase.java:1252)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneBase.getAllCellInfo(PhoneBase.java:1240)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneProxy.getAllCellInfo(PhoneProxy.java:337)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.phone.PhoneInterfaceManager.getAllCellInfo(PhoneInterfaceManager.java:1486)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.ITelephony$Stub.onTransact(ITelephony.java:691)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at android.os.Binder.execTransact(Binder.java:446)
第一个问题可能是您没有在中添加INTERNET
权限AndroidManifest.xml
。要解决此问题,只需<uses-permission android:name="android.permission.INTERNET" />
在清单中添加这一行。
在另一个问题是,你要执行的主线程POST请求。它导致NetworkOnMainThreadException
。您应该使用AsyncTask
或Handler
执行网络请求,如下所示:
public class MainActivity extends ActionBarActivity {
private static final String TAG = "victorMessage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
new HttpPostTask().execute();
}
private class HttpPostTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://93.117.158.187/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.i(TAG, response.toString());
return response.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
//do something with you response, connected with UI thread.
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句