【问题标题】:HttpHostConnectException + NullPointerExceptionHttpHostConnectException + NullPointerException
【发布时间】:2015-03-27 09:55:37
【问题描述】:

我正在尝试从我机器上的 mySQL 数据库中获取数据。当我在模拟器上朗姆应用程序时,我得到一个HttpHostConnectException(我已经在浏览器中测试了我的地址,并尝试了带有和不带端口号的 10.0.2.2)然后应用程序因NullPointerException 而崩溃。我该如何解决这些问题?

连接器.java

public class Connector {

public JSONArray getResults(){

    String url = "http://192.168.87.1/android/getStudents.php";

    HttpEntity httpentity = null;

    try
    {
        DefaultHttpClient httpclient =new DefaultHttpClient();
        HttpGet httpget= new HttpGet(url);
        HttpResponse httpresponse = httpclient.execute(httpget);
        httpentity = httpresponse.getEntity();

    }catch (ClientProtocolException e){
        e.printStackTrace();

    }catch (IOException e){
        e.printStackTrace();
    }

    JSONArray jsonarray = null;
    if(httpentity != null){
        try{
            String entityresponse = EntityUtils.toString(httpentity);
            Log.e("Entity Response: ", entityresponse);

            jsonarray = new JSONArray(entityresponse);

        }catch (JSONException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    return jsonarray;
}}

MainActivity.java

public class MainActivity extends ActionBarActivity {

private TextView resultsViewer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.resultsViewer = (TextView)this.findViewById(R.id.resultsViewer);

    new getResultsTask().execute(new Connector());
}


private void setText (JSONArray jsonarray){

    String string = "";
    for(int i=0; i<jsonarray.length(); i++){
        JSONObject json = null;
        try{
            json = jsonarray.getJSONObject(i);

            string = string + "Exam : " + json.getString("exam_name") + "\n" +
                              "Score :" + json.getString("score") + "\n\n";

        }catch (JSONException e){
            e.printStackTrace();
        }
    }
    this.resultsViewer.setText(string);
}

private class getResultsTask extends AsyncTask<Connector, Long, JSONArray>{

    @Override
    protected JSONArray doInBackground(Connector... params) {

                 return params[0].getResults();
    }

    @Override
    protected void onPostExecute(JSONArray jsonarray) {

        setText(jsonarray);
    }

}}

LogCat 中的一些行

Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f09003f}
 Emulator without GPU emulation detected.
 Background sticky concurrent mark sweep GC freed 3259(270KB) AllocSpace objects, 0(0B) LOS objects, 31% free, 759KB/1117KB, paused 3.009ms total 1.004s
 org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.56.1 refused
 at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:188)

和 NullPointerExeption

    Shutting down VM
 --------- beginning of crash
 FATAL EXCEPTION: main
 Process: com.ed.jsontest2, PID: 1302
 java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
    at com.ed.jsontest2.MainActivity.setText(MainActivity.java:32)

【问题讨论】:

    标签: android mysql nullpointerexception httphostconnectexception


    【解决方案1】:

    如果服务器在本地计算机上运行,​​则必须使用 ip 地址 10.0.0.2 。端口号是必需的

    你的网址应该是http://10.0.2.2:portnumber/android/getStudents.php...

    端口号是您本地服务器运行的端口

    如果您是从远程机器运行服务器,则替换主机 ipaddress

    【讨论】:

    • 我已经做出了改变。我仍然遇到同样的错误。
    • 您正在运行的服务的端口号是多少?本地机器还是远程机器?
    • 首先从您的浏览器检查localhost:80/android/getStudents.php ...如果这不起作用,那么问题将出在您的服务器端
    • 它在浏览器中有效,但在应用程序中仍然无效
    • 仍然没有变化。 :( 我们缺少什么?
    猜你喜欢
    • 2015-03-14
    • 1970-01-01
    • 2013-03-10
    • 2013-05-03
    • 1970-01-01
    • 2017-09-13
    • 2012-05-27
    • 2012-07-15
    • 2023-03-18
    相关资源
    最近更新 更多