1

我正在创建一个可以访问外部数据库以检索一些数据的移动应用程序,但问题出在 logcat 我得到错误任何人都可以帮助我,我将不胜感激

TestExternalDatabaseActivity.java

package com.taseen.apps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.array;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class TestExternalDatabaseActivity extends Activity {

    TextView resultView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testexternaldatabase);

    resultView =(TextView)findViewById(R.id.result);
    getData();



    }


   public void getData() 
   {
       String  result = "";
       InputStream isr = null;

       try {
        HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new  HttpPost("127.0.0.2/getAllCustomers.php");
           HttpResponse response  = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
           try {
            isr = entity.getContent();
        } catch (IllegalStateException e4) {
            // TODO Auto-generated catch block
            e4.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Log.e("log-tag", "Error in  connection"+e.toString());
        resultView.setText("could not connect to database");


        //convert response to string 

        try {
            BufferedReader reader = new  BufferedReader(new InputStreamReader(isr, "iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = reader.readLine())!= null)
            {
                sb.append(line + "\n");

            }
            isr.close();

            result = sb.toString();
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            Log.e("Log-tag", "Error converting  result" +e.toString());




    //parse json data

            try {
                String s ="";
                JSONArray jArray = new JSONArray(result);

                for(int i = 0; i<jArray.length(); i++)
                {
                    JSONObject json = jArray.getJSONObject(i);
                    s =  s +
                            "Name: "+json.getString("FirstName")+" "+json.getString("LastName")+"\n"+
                            "Age: "+json.getString("Age")+"\n"+
                            "Mobile Used: "+json.getString("Mobile")+"\n\n";

                }

                resultView.setText(s);
            } catch (JSONException e2) {
                // TODO Auto-generated catch block
                Log.e("log-tag", "Error parsing data"+e.toString());
            }


    } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }





   } catch (IOException e5) {
        // TODO Auto-generated catch block
        e5.printStackTrace();
    }

  }
}

日志猫:

04-02 19:12:24.933: D/AndroidRuntime(331): Shutting down VM
04-02 19:12:24.933: W/dalvikvm(331): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-02 19:12:24.952: E/AndroidRuntime(331): FATAL EXCEPTION: main
04-02 19:12:24.952: E/AndroidRuntime(331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.taseen.apps/com.taseen.apps.TestExternalDatabaseActivity}: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.os.Looper.loop(Looper.java:123)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 19:12:24.952: E/AndroidRuntime(331):  at java.lang.reflect.Method.invokeNative(Native Method)
04-02 19:12:24.952: E/AndroidRuntime(331):  at java.lang.reflect.Method.invoke(Method.java:507)
04-02 19:12:24.952: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 19:12:24.952: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 19:12:24.952: E/AndroidRuntime(331):  at dalvik.system.NativeStart.main(Native Method)
04-02 19:12:24.952: E/AndroidRuntime(331): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
04-02 19:12:24.952: E/AndroidRuntime(331):  at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:572)
04-02 19:12:24.952: E/AndroidRuntime(331):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
04-02 19:12:24.952: E/AndroidRuntime(331):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-02 19:12:24.952: E/AndroidRuntime(331):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-02 19:12:24.952: E/AndroidRuntime(331):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-02 19:12:24.952: E/AndroidRuntime(331):  at com.taseen.apps.TestExternalDatabaseActivity.getData(TestExternalDatabaseActivity.java:51)
04-02 19:12:24.952: E/AndroidRuntime(331):  at com.taseen.apps.TestExternalDatabaseActivity.onCreate(TestExternalDatabaseActivity.java:36)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 19:12:24.952: E/AndroidRuntime(331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-02 19:12:24.952: E/AndroidRuntime(331):  ... 11 more

获取所有客户.php

<?php

$con = mysql_connect("localhost", "root", "root")or die(mysql_error());

mysql_select_db("TestDatabase", $con) or die("can not connect to the database");
$result = mysql_query("SELECT * FROM customer")or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    $output[] = $row;
}
  print(json_encode($output));

  mysql_close();

?>
4

2 回答 2

0

我认为您的问题是您忘记了http://前缀尝试以下代码:

HttpPost httppost = new  HttpPost("http://192.168.0.1/getAllCustomers.php");

请注意,您还需要设置 localhost 的实际服务器 ip instad。

于 2013-04-02T19:25:43.000 回答
0

错误告诉你问题...

java.lang.IllegalStateException:目标主机不能为空,或在参数中设置。

你确定你的意思是使用127.0.0.2?您是否有在该 IP 地址上运行的 PHP 服务器?

于 2013-04-02T19:26:30.013 回答