我是android的新手,当我尝试插入数据时 我的android应用程序到localhost MySQL数据库,App正在自动终止。 MainActivity.java
public class MainActivity extends Activity
{
EditText ET_id,ET_FIRSTPLACE,ET_SECONDPLACE,ET_DISTANCE,ET_TIME2REACH,ET_FARE;
Button BT_SUBMIT;
String id,firstplace,secondplace,distance,time2reach,fare;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET_id=(EditText)findViewById(R.id.id);
ET_FIRSTPLACE=(EditText)findViewById(R.id.firstplace);
ET_SECONDPLACE=(EditText)findViewById(R.id.secondplace);
ET_DISTANCE=(EditText)findViewById(R.id.distance);
ET_TIME2REACH=(EditText)findViewById(R.id.time2reach);
ET_FARE=(EditText)findViewById(R.id.fare);
BT_SUBMIT=(Button)findViewById(R.id.submit);
}
public void submit(View view)
{
id=ET_id.getText().toString();
firstplace=ET_FIRSTPLACE.getText().toString();
secondplace=ET_SECONDPLACE.getText().toString();
distance =ET_DISTANCE.getText().toString();
time2reach=ET_TIME2REACH.getText().toString();
fare=ET_FARE.getText().toString();
String method="submit";
BackgroundTask backgroundTask= new BackgroundTask(this);
backgroundTask.execute(method,id,firstplace,secondplace,distance,time2reach,fare);
finish();
}
}
BackgroundTask.java
/**
* Created by Sumedh on 29/08/2015.
*/
public class BackgroundTask extends AsyncTask <String,Void,String> {
Context ctx;
BackgroundTask(Context ctx) {
this.ctx = ctx;
}
protected String doInBackground(String... params)
{
String reg_url = "http://10.0.3.2/busspotter/insert.php";
String method=params[0];
String id =params[1];
String firstplace =params[2];
String secondplace =params[3];
String distance =params[4];
String time2reach =params[5];
String fare =params[6];
if(method.equals("submit")) {
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
//httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
URLEncoder.encode("firstplace","UTF-8")+"="+URLEncoder.encode(firstplace,"UTF-8")+"&"+
URLEncoder.encode("secondplace","UTF-8")+"="+URLEncoder.encode(secondplace,"UTF-8")+"&"+
URLEncoder.encode("distance","UTF-8")+"="+URLEncoder.encode(distance,"UTF-8")+"&"+
URLEncoder.encode("time2reach","UTF-8")+"="+URLEncoder.encode(time2reach,"UTF-8")+"&"+
URLEncoder.encode("fare","UTF-8")+"="+URLEncoder.encode(fare,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();
httpURLConnection.disconnect();
return "Inserted Succesfully...";
} catch (MalformedInputException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if (result.equals("Inserted Succesfully...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
}
这是我的logCat -
08-30 04:57:49.368 17491-17491/? D/dalvikvm﹕ Late-enabling CheckJNI
08-30 04:57:49.392 17491-17497/? E/jdwp﹕ Failed sending reply to debugger:Broken pipe
08-30 04:57:49.392 17491-17497/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
08-30 04:57:49.596 17491-17491/? D/libEGL﹕ loaded/system/lib/egl/libEGL_genymotion.so
08-30 04:57:49.600 17491-17491/? D/﹕ HostConnection::get() New Host Connection established 0xb943bea0, tid 17491
08-30 04:57:49.624 17491-17491/? D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
08-30 04:57:49.624 17491-17491/? D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
08-30 04:57:49.700 17491-17491/? W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
08-30 04:57:49.704 17491-17491/? E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
08-30 04:57:49.708 17491-17491/? E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
08-30 04:57:49.716 17491-17491/? E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-30 04:57:49.724 17491-17491/? E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
08-30 04:57:49.728 17491-17491/? D/OpenGLRenderer﹕ Enabling debug mode 0
08-30 04:57:59.983 17491-17506/com.example.sumedh.busspotterinsert D/dalvikvm﹕ GC_FOR_ALLOC freed 185K, 9% free 3043K/3316K, paused 5ms, total 5ms
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ java.net.SocketException: socket failed: EACCES (Permission denied)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:576)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at java.net.Socket.checkOpenAndCreate(Socket.java:664)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at java.net.Socket.connect(Socket.java:808)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:101)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
08-30 04:57:59.987 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.example.sumedh.busspotterinsert.BackgroundTask.doInBackground(BackgroundTask.java:49)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at com.example.sumedh.busspotterinsert.BackgroundTask.doInBackground(BackgroundTask.java:18)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at libcore.io.Posix.socket(Native Method)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:561)
08-30 04:57:59.991 17491-17506/com.example.sumedh.busspotterinsert W/System.err﹕ ... 19 more
08-30 04:58:00.071 17491-17491/com.example.sumedh.busspotterinsert D/AndroidRuntime﹕ Shutting down VM
08-30 04:58:00.071 1749117491/com.example.sumedh.busspotterinsertW/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cf2b20)
08-30 04:58:00.071 17491-17491/com.example.sumedh.busspotterinsert E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sumedh.busspotterinsert, PID: 17491
java.lang.NullPointerException
at com.example.sumedh.busspotterinsert.BackgroundTask.onPostExecute(BackgroundTask.java:85)
at com.example.sumedh.busspotterinsert.BackgroundTask.onPostExecute(BackgroundTask.java:18)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你的
protected String doInBackground(String... params)
方法返回null,并且onPostExecute(String result)尝试从NULL对象读取值。
尝试添加此
if (result != null && result.equals("Inserted Succesfully...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} else Toast.makeText(ctx, "Something went wrong", Toast.LENGTH_LONG).show();