Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HTTP POST到MySQL数据库AyncTask错误。强行关闭_Java_Android_Mysql_Json - Fatal编程技术网

Java HTTP POST到MySQL数据库AyncTask错误。强行关闭

Java HTTP POST到MySQL数据库AyncTask错误。强行关闭,java,android,mysql,json,Java,Android,Mysql,Json,我正在尝试连接到服务器上的数据库。自从向我推荐AsyncTask以来,我才刚刚开始使用它。在此之前,我在主UI线程上有HTTP请求,这当然意味着它无法工作。现在情况好转了一点,但我仍然有问题。如果我的服务器关闭,我会得到一个强制关闭。如果你看我下面的代码,我会举杯祝酒,告诉我连接是否无法建立。然而,这并没有表现出来 有什么我需要改变的,以使它停止强行关闭,只是显示祝酒辞 当我打开服务器,数据库启动时,我查看了logcat,我的android应用程序确实检索到了数据,但数据在logcat中,没有显

我正在尝试连接到服务器上的数据库。自从向我推荐AsyncTask以来,我才刚刚开始使用它。在此之前,我在主UI线程上有HTTP请求,这当然意味着它无法工作。现在情况好转了一点,但我仍然有问题。如果我的服务器关闭,我会得到一个强制关闭。如果你看我下面的代码,我会举杯祝酒,告诉我连接是否无法建立。然而,这并没有表现出来

有什么我需要改变的,以使它停止强行关闭,只是显示祝酒辞

当我打开服务器,数据库启动时,我查看了logcat,我的android应用程序确实检索到了数据,但数据在logcat中,没有显示在页面上。其中一些代码来自教程和谷歌搜索,因此我知道“//解析数据”位只计算当前数据的长度,而不计算其他内容。我想这就是我需要添加代码来显示它的地方。只是想让我知道这对我来说是多么新鲜

真的对这一切感到沮丧,这绝对没有帮助:(

这是我的密码:

package com.android.history;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CurrentSeasonDrivers extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.currentseason_drivers);

    new HttpTask().execute();

}

private static class HttpTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... params) {
        doStuff();

        return null;
    }

    public static void doStuff() {
        JSONArray jArray;
        String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        HttpResponse response;
        HttpEntity entity;

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        // HTTP POST REQUEST
        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("http://192.168.0.13/testdatabase.php");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpclient.execute(httppost);

            entity = response.getEntity();

            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection" + e.toString());
            Toast.makeText(null, "Could not connect to server", Toast.LENGTH_LONG).show();
        }

        // CONVERT RESPONSE TO STRING
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            is.close();
            result = sb.toString();

            Log.i("json string", result);
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }


        // PARSING DATA
        String drivername;
        String drivesfor;
        try {
            jArray = new JSONArray(result);
            JSONObject json_data = null;

            System.out.println("Length " + jArray.length());
            Log.d("DB", "Length " + jArray.length());

            for (int i = 0; i < jArray.length(); i++) {

                System.out.println("counter " + i);
                json_data = jArray.getJSONObject(i);
                drivername = json_data.getString("Driver_full_name");
                drivesfor = json_data.getString("Drives_for");

                System.out.println("Drives_for" + drivesfor);
            }
        } catch (JSONException e1) {
            Log.d("DB", "Error somewhere");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
    }
}
}
package com.android.history;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.ParseException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.Toast;
公共类驱动程序扩展活动{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.CurrentU驱动程序);
新建HttpTask().execute();
}
私有静态类HttpTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的Void doInBackground(Void…参数){
doStuff();
返回null;
}
公共静态void doStuff(){
杰索纳雷·贾雷;
字符串结果=null;
InputStream=null;
StringBuilder sb=null;
HttpResponse响应;
http实体;
ArrayList nameValuePairs=新的ArrayList();
//HTTP POST请求
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://192.168.0.13/testdatabase.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
entity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
Toast.makeText(null,“无法连接到服务器”,Toast.LENGTH_LONG.show();
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
弦线;
而((line=reader.readLine())!=null){
某人附加(行);
}
is.close();
结果=sb.toString();
Log.i(“json字符串”,结果);
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析数据
字符串驱动程序名;
字符串驱动器;
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
System.out.println(“长度”+jArray.Length());
Log.d(“DB”,“Length”+jArray.Length());
for(int i=0;i
以下是我的日志目录中的错误:

08-22 12:26:39.405: E/log_tag(14327): Error in http connectionorg.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.13 refused
08-22 12:26:39.405: W/dalvikvm(14327): threadid=13: thread exiting with uncaught exception (group=0x40a051f8)
08-22 12:26:39.455: E/AndroidRuntime(14327): FATAL EXCEPTION: AsyncTask #2
08-22 12:26:39.455: E/AndroidRuntime(14327): java.lang.RuntimeException: An error occured while executing doInBackground()
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.lang.Thread.run(Thread.java:856)
08-22 12:26:39.455: E/AndroidRuntime(14327): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.Handler.<init>(Handler.java:121)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.widget.Toast$TN.<init>(Toast.java:317)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.widget.Toast.<init>(Toast.java:91)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.widget.Toast.makeText(Toast.java:233)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at com.android.history.CurrentSeasonDrivers$HttpTask.doStuff(CurrentSeasonDrivers.java:74)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at com.android.history.CurrentSeasonDrivers$HttpTask.doInBackground(CurrentSeasonDrivers.java:44)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at com.android.history.CurrentSeasonDrivers$HttpTask.doInBackground(CurrentSeasonDrivers.java:1)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-22 12:26:39.455: E/AndroidRuntime(14327):    ... 5 more
08-22 12:26:39.405:E/log_标记(14327):http connectionorg.apache.http.conn.HttpHostConnectException:连接到http://192.168.0.13 拒绝
08-22 12:26:39.405:W/dalvikvm(14327):threadid=13:线程退出时出现未捕获异常(组=0x40a051f8)
08-22 12:26:39.455:E/AndroidRuntime(14327):致命异常:AsyncTask#2
08-22 12:26:39.455:E/AndroidRuntime(14327):java.lang.RuntimeException:执行doInBackground()时出错
08-22 12:26:39.455:E/AndroidRuntime(14327):在android.os.AsyncTask$3.done(AsyncTask.java:278)
08-22 12:26:39.455:E/AndroidRuntime(14327):位于java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-22 12:26:39.455:E/AndroidRuntime(14327):位于java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-22 12:26:39.455:E/AndroidRuntime(14327):位于java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-22 12:26:39.455:E/AndroidRuntime(14327):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-22 12:26:39.455:E/AndroidRuntime(14327):在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-22 12:26:39.455:E/AndroidRuntime(14327):位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-22 12:26:39
 runOnUiThread(new Runnable() {          
   public void run() {                 
    Toast.makeText(null, "Could not connect to server", Toast.LENGTH_LONG).show();     
 }            
}); 
jArray = new JSONArray(result);
if(null != result){
     jArray = new JSONArray(result);
}
else{ 
    //Do something else 
      }