我有一个应用程序应该发送电话号码到PHP Web服务并获得响应。 在我的代码中有一个按钮,它应该执行另一个扩展AsyncTask类的类。
按下按钮时应用程序崩溃的问题。
public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject>
{
String url = "http://192.168.1.6:89/Derdeery/banks.php";
protected JSONObject doInBackground(JSONObject... data) {
JSONObject json = data[0];
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
JSONObject jsonResponse = null;
HttpPost post = new HttpPost(url);
try {
StringEntity se = new StringEntity("json="+json.toString());
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(se);
HttpResponse response;
response = client.execute(post);
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
jsonResponse=new JSONObject(resFromServer);
Log.i("Response from server", jsonResponse.getString("msg"));
} catch (Exception e) { e.printStackTrace();}
return jsonResponse;
}
}
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1) ;
b .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
JSONObject toSend = new JSONObject();
toSend.put("msg", 1);
Toast.makeText(getApplicationContext(), mymethod(),Toast.LENGTH_LONG );
JSONTransmitter transmitter = new JSONTransmitter();
transmitter.execute(new JSONObject[] {toSend});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
logcat的
12-30 09:02:57.734: W/dalvikvm(792): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-30 09:02:57.864: E/AndroidRuntime(792): FATAL EXCEPTION: main
12-30 09:02:57.864: E/AndroidRuntime(792): java.lang.NullPointerException
12-30 09:02:57.864: E/AndroidRuntime(792): at com.coffeecodes.coffecodesdoodle.MainActivity.mymethod(MainActivity.java:49)
12-30 09:02:57.864: E/AndroidRuntime(792): at com.coffeecodes.coffecodesdoodle.MainActivity$1.onClick(MainActivity.java:34)
12-30 09:02:57.864: E/AndroidRuntime(792): at android.view.View.performClick(View.java:4202)
12-30 09:02:57.864: E/AndroidRuntime(792): at android.view.View$PerformClick.run(View.java:17340)
12-30 09:02:57.864: E/AndroidRuntime(792): at android.os.Handler.handleCallback(Handler.java:725)
12-30 09:02:57.864: E/AndroidRuntime(792): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 09:02:57.864: E/AndroidRuntime(792): at android.os.Looper.loop(Looper.java:137)
12-30 09:02:57.864: E/AndroidRuntime(792): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 09:02:57.864: E/AndroidRuntime(792): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 09:02:57.864: E/AndroidRuntime(792): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 09:02:57.864: E/AndroidRuntime(792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 09:02:57.864: E/AndroidRuntime(792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 09:02:57.864: E/AndroidRuntime(792): at dalvik.system.NativeStart.main(Native Method)
12-30 09:02:58.146: D/dalvikvm(792): GC_CONCURRENT freed 389K, 16% free 2833K/3340K, paused 74ms+26ms, total 390ms
12-30 09:05:35.477: I/Process(792): Sending signal. PID: 792 SIG: 9
答案 0 :(得分:0)
是否可以将此行StringEntity se = new StringEntity("json="+json.toString());
替换为StringEntity se = new StringEntity(json.toString());
我的问题是您是否在保存之前将电话号码验证到您的网络服务中,如果是,请发送到JSONObject toSend=new JSONObject(); toSend.put("msg", Phone_Number);
答案 1 :(得分:0)
`12-30 09:02:57.864: E/AndroidRuntime(792): java.lang.NullPointerException
12-30 09:02:57.864: E/AndroidRuntime(792): at com.coffeecodes.coffecodesdoodle.MainActivity.mymethod(MainActivity.java:49)`
问题出在MainActivity.java文件的第49行。 也许你应该删除这个Toast。 你甚至不会在这个吐司上调用show()方法。
Toast.makeText(getApplicationContext(), mymethod(),Toast.LENGTH_LONG );
应该是:
Toast.makeText(getApplicationContext(), mymethod(),Toast.LENGTH_LONG ).show();