我想在从服务器获得 JSON 响应后显示一个消息框。在下面给出的代码中,数据库正在正确更新,但未显示消息框。它只是显示“不幸的是,myapp 已停止”消息。我不知道实际在哪里代码出错。在编码中发现NOo 错误。这是我的 .java 文件
package com.example.loga;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
public class MainActivity1 extends Activity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputuserid;
EditText inputserverid;
private static String url_register = "http://10.0.2.2/android_connect/register.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
// Edit Text
inputuserid = (EditText) findViewById(R.id.editText1);
inputserverid= (EditText) findViewById(R.id.editText2);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
// creating new product in background thread
new add().execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity1, menu);
return true;
}
class add extends AsyncTask<String, String,String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity1.this);
pDialog.setMessage("your Registration is processing..wait for few sec..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
String userid = inputuserid.getText().toString();
String serverid = inputserverid.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("userid",userid));
params.add(new BasicNameValuePair("serverid", serverid));
JSONObject json = jsonParser.makeHttpRequest(url_register,
"POST", params);
Log.d("Create Response", json.toString());
try
{
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(MainActivity1.this).create();
alertDialog.setTitle("password verification..");
alertDialog.setMessage("success.!!");
alertDialog.show();
finish();
} else {
// failed to create product
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
我的PHP文件是:
<?php
$response = array();
// check for required fields
if (isset($_POST['userid']) && isset($_POST['serverid']))
{
$userid = $_POST['userid'];
$serverid= $_POST['serverid'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO opass(User_ID, Server_ID ) VALUES('$userid', '$serverid')");
// check if row inserted or not
if ($result)
{
// successfully inserted into database
$response["success"] = 1;
$response["message"] = " Registered successfully";
// echoing JSON response
echo json_encode($response);
}
else
{
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
else
{
// required field is missing
$response["success"] = 1;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
所以我的logcat错误:
04-21 17:00:14.941: I/Choreographer(27578): Skipped 89 frames! The application may be doing too much work on its main thread.
.
.
.
.
.
4-21 17:00:14.921: E/AndroidRuntime(27578): FATAL EXCEPTION: AsyncTask #2
04-21 17:00:14.921: E/AndroidRuntime(27578): java.lang.RuntimeException: An error occured while executing doInBackground()
04-21 17:00:14.921: E/AndroidRuntime(27578): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-21 17:00:14.921: E/AndroidRuntime(27578): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-21 17:00:14.921: E/AndroidRuntime(27578): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-21 17:00:14.921: E/AndroidRuntime(27578): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-21 17:00:14.921: E/AndroidRuntime(27578): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-21 17:00:14.921: E/AndroidRuntime(27578): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-21 17:00:14.921: E/AndroidRuntime(27578): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-21 17:00:14.921: E/AndroidRuntime(27578): at java.lang.Thread.run(Thread.java:856)
04-21 17:00:14.921: E/AndroidRuntime(27578): Caused by: java.lang.NullPointerException
04-21 17:00:14.921: E/AndroidRuntime(27578): at com.example.loga.MainActivity1$add.doInBackground(MainActivity1.java:97)
04-21 17:00:14.921: E/AndroidRuntime(27578): at com.example.loga.MainActivity1$add.doInBackground(MainActivity1.java:1)
04-21 17:00:14.921: E/AndroidRuntime(27578): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-21 17:00:14.921: E/AndroidRuntime(27578): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-21 17:00:14.921: E/AndroidRuntime(27578): ... 4 more
04-21 17:00:14.992: I/Choreographer(27578): Skipped 44 frames! The application may be doing too much work on its main thread.
04-21 17:00:17.361: I/Choreographer(27578): Skipped 134 frames! The application may be doing too much work on its main thread.
04-21 17:00:17.941: E/WindowManager(27578): Activity com.example.loga.MainActivity1 has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d28880 V.E..... R.....ID 0,0-240,77} that was originally added here
04-21 17:00:17.941: E/WindowManager(27578): android.view.WindowLeaked: Activity com.example.loga.MainActivity1 has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d28880 V.E..... R.....ID 0,0-240,77} that was originally added here
04-21 17:00:17.941: E/WindowManager(27578): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
04-21 17:00:17.941: E/WindowManager(27578): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
04-21 17:00:17.941: E/WindowManager(27578): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-21 17:00:17.941: E/WindowManager(27578): at android.app.Dialog.show(Dialog.java:281)
04-21 17:00:17.941: E/WindowManager(27578): at com.example.loga.MainActivity1$add.onPreExecute(MainActivity1.java:81)
04-21 17:00:17.941: E/WindowManager(27578): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-21 17:00:17.941: E/WindowManager(27578): at android.os.AsyncTask.execute(AsyncTask.java:534)
04-21 17:00:17.941: E/WindowManager(27578): at com.example.loga.MainActivity1$1.onClick(MainActivity1.java:58)
04-21 17:00:17.941: E/WindowManager(27578): at android.view.View.performClick(View.java:4204)
04-21 17:00:17.941: E/WindowManager(27578): at android.view.View$PerformClick.run(View.java:17355)
04-21 17:00:17.941: E/WindowManager(27578): at android.os.Handler.handleCallback(Handler.java:725)
04-21 17:00:17.941: E/WindowManager(27578): at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 17:00:17.941: E/WindowManager(27578): at android.os.Looper.loop(Looper.java:137)
04-21 17:00:17.941: E/WindowManager(27578): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 17:00:17.941: E/WindowManager(27578): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:00:17.941: E/WindowManager(27578): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 17:00:17.941: E/WindowManager(27578): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 17:00:17.941: E/WindowManager(27578): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 17:00:17.941: E/WindowManager(27578): at dalvik.system.NativeStart.main(Native Method)
04-21 17:00:17.941: I/Choreographer(27578): Skipped 317 frames! The application may be doing too much work on its main thread.
04-21 17:05:16.065: I/Process(27578): Sending signal. PID: 27578 SIG: 9
04-21 17:16:06.902: E/Trace(27969): error opening trace file: No such file or directory (2)
04-21 17:16:08.042: I/Choreographer(27969): Skipped 107 frames! The application may be doing too much work on its main thread.
04-21 17:16:08.122: D/gralloc_goldfish(27969): Emulator without GPU emulation detected.
04-21 17:16:47.102: I/Choreographer(27969): Skipped 41 frames! The application may be doing too much work on its main thread.
04-21 17:16:48.292: I/Choreographer(27969): Skipped 82 frames! The application may be doing too much work on its main thread.
04-21 17:16:49.062: I/Choreographer(27969): Skipped 224 frames! The application may be doing too much work on its main thread.
04-21 17:16:52.293: I/Choreographer(27969): Skipped 92 frames! The application may be doing too much work on its main thread.
04-21 17:16:52.652: D/dalvikvm(27969): GC_CONCURRENT freed 152K, 11% free 2610K/2904K, paused 7ms+111ms, total 231ms
04-21 17:16:53.564: I/Choreographer(27969): Skipped 89 frames! The application may be doing too much work on its main thread.
04-21 17:16:54.021: D/InputEventConsistencyVerifier(27969): KeyEvent: ACTION_UP but key was not down.
04-21 17:16:54.021: D/InputEventConsistencyVerifier(27969): in android.widget.EditText{40d27d40 VFED..CL .F....I. 90,74-228,104 #7f080007 app:id/editText2}
04-21 17:16:54.021: D/InputEventConsistencyVerifier(27969): 0: sent at 109014528000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=109014528, downTime=109014528, deviceId=0, source=0x101 }
04-21 17:16:54.721: I/Choreographer(27969): Skipped 109 frames! The application may be doing too much work on its main thread.
04-21 17:16:56.826: I/Choreographer(27969): Skipped 89 frames! The application may be doing too much work on its main thread.
04-21 17:16:57.531: I/Choreographer(27969): Skipped 275 frames! The application may be doing too much work on its main thread.
04-21 17:16:57.831: D/AndroidRuntime(27969): Shutting down VM
04-21 17:16:57.831: W/dalvikvm(27969): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-21 17:16:57.981: E/AndroidRuntime(27969): FATAL EXCEPTION: main
04-21 17:16:57.981: E/AndroidRuntime(27969): android.os.NetworkOnMainThreadException
04-21 17:16:57.981: E/AndroidRuntime(27969): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-21 17:16:57.981: E/AndroidRuntime(27969): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-21 17:16:57.981: E/AndroidRuntime(27969): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-21 17:16:57.981: E/AndroidRuntime(27969): at libcore.io.IoBridge.connect(IoBridge.java:112)
04-21 17:16:57.981: E/AndroidRuntime(27969): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-21 17:16:57.981: E/AndroidRuntime(27969): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-21 17:16:57.981: E/AndroidRuntime(27969): at java.net.Socket.connect(Socket.java:842)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-21 17:16:57.981: E/AndroidRuntime(27969): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-21 17:16:57.981: E/AndroidRuntime(27969): at com.example.loga.JSONParser.makeHttpRequest(JSONParser.java:51)
04-21 17:16:57.981: E/AndroidRuntime(27969): at com.example.loga.MainActivity1$add.onPostExecute(MainActivity1.java:97)
04-21 17:16:57.981: E/AndroidRuntime(27969): at com.example.loga.MainActivity1$add.onPostExecute(MainActivity1.java:1)
04-21 17:16:57.981: E/AndroidRuntime(27969): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-21 17:16:57.981: E/AndroidRuntime(27969): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-21 17:16:57.981: E/AndroidRuntime(27969): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-21 17:16:57.981: E/AndroidRuntime(27969): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 17:16:57.981: E/AndroidRuntime(27969): at android.os.Looper.loop(Looper.java:137)
04-21 17:16:57.981: E/AndroidRuntime(27969): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 17:16:57.981: E/AndroidRuntime(27969): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:16:57.981: E/AndroidRuntime(27969): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 17:16:57.981: E/AndroidRuntime(27969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 17:16:57.981: E/AndroidRuntime(27969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 17:16:57.981: E/AndroidRuntime(27969): at dalvik.system.NativeStart.main(Native Method)
04-21 17:16:58.221: D/dalvikvm(27969): GC_CONCURRENT freed 214K, 12% free 2812K/3164K, paused 71ms+19ms, total 322ms
04-21 17:17:01.412: I/Process(27969): Sending signal. PID: 27969 SIG: 9
04-21 17:17:02.322: E/Trace(28003): error opening trace file: No such file or directory (2)
04-21 17:17:02.862: I/Choreographer(28003): Skipped 125 frames! The application may be doing too much work on its main thread.
04-21 17:17:02.892: D/gralloc_goldfish(28003): Emulator without GPU emulation detected.
帮助我找出我做的任何错误。谢谢:)