android + php mysql

I am creating a mobile application that have access to an external database to retrieve some data but the problem is in the logcat I got error can anyone help me I will appreciate that

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 + "
");

            }
            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")+"
"+
                            "Age: "+json.getString("Age")+"
"+
                            "Mobile Used: "+json.getString("Mobile")+"

";

                }

                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();
    }

  }
}

the logcat:

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

getAllCustomers.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();

?>
duande1146
duande1146 错误日志表明未设置目标主机。
7 年多之前 回复
dsadsa123111
dsadsa123111 请不要在新代码中使用mysql_*函数。它们不再维护,并且已被正式弃用。看到红色的盒子?了解准备好的语句,并使用PDO或MySQLi-本文将帮助您确定哪些。如果您选择PDO,这是一个很好的教程。
7 年多之前 回复

2个回答



我认为您的问题是您忘记了 http:// </ code>前缀尝试此代码:</ p >

  HttpPost httppost = new HttpPost(“http://192.168.0.1/getAllCustomers.php”); 
</ code> </ pre>

注意 你还需要设置localhost的实际服务器ip instad。</ p>
</ div>

展开原文

原文

I think your problem is that you forgot the http:// prefix try this code:

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

Note that you also need to set the actual server ip instad of localhost.

duanjuhuo8772
duanjuhuo8772 kabuto你的意思是我的代码没有关闭?
7 年多之前 回复
drbi19093
drbi19093 你的意思是我的代码? 我看不到语法错误。
7 年多之前 回复
doulao2128
doulao2128 我认为你的一些代码也没有正确关闭。
7 年多之前 回复
duanlin1931
duanlin1931 如果我猜对了变量结果中的字符串,则需要使用如下代码将其设置为TextView:((TextView)findViewById(R.id.text_view_1))。setText(result);
7 年多之前 回复
douzi5214
douzi5214 这是错误的http:现在应用程序运行但我没有得到任何东西只是“textView”
7 年多之前 回复
doujian0265
doujian0265 rekire我正在使用localhost所以我需要在localhost上运行它
7 年多之前 回复



错误告诉您问题... </ p>


java.lang .IllegalStateException:目标主机不能为空,或者在参数中设置。</ p>
</ blockquote>

您确定要使用 127.0.0.2 </ code>吗? 你有一个在该IP地址运行的PHP服务器吗?</ p>
</ div>

展开原文

原文

The error tells you the problem...

java.lang.IllegalStateException: Target host must not be null, or set in parameters.

Are you sure you mean to use 127.0.0.2? Do you have a PHP server running at that IP Address?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐