我正在创建一个可以访问外部数据库以检索一些数据的移动应用程序,但问题出在日志猫中我收到错误任何人都可以帮助我,我将不胜感激
如日志 cat 系统错误在 * HttpResponse response = httpclient.execute(httppost); *那么如何解决这个问题?
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 20:47:58.223: E/log-tag(699): before the first try
04-02 20:47:58.223: E/log-tag(699): after the httpclient
04-02 20:47:58.282: E/log-tag(699): after the httppostt
04-02 20:47:58.352: W/System.err(699): org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.2 refused
04-02 20:47:58.362: W/System.err(699): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
04-02 20:47:58.362: W/System.err(699): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-02 20:47:58.362: W/System.err(699): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-02 20:47:58.362: W/System.err(699): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
04-02 20:47:58.372: W/System.err(699): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-02 20:47:58.372: W/System.err(699): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-02 20:47:58.372: W/System.err(699): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-02 20:47:58.372: W/System.err(699): at com.taseen.apps.TestExternalDatabaseActivity.getData(TestExternalDatabaseActivity.java:60)
04-02 20:47:58.372: W/System.err(699): at com.taseen.apps.TestExternalDatabaseActivity.onCreate(TestExternalDatabaseActivity.java:38)
04-02 20:47:58.372: W/System.err(699): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 20:47:58.372: W/System.err(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-02 20:47:58.372: W/System.err(699): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-02 20:47:58.475: W/System.err(699): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 20:47:58.475: W/System.err(699): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-02 20:47:58.475: W/System.err(699): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 20:47:58.475: W/System.err(699): at android.os.Looper.loop(Looper.java:123)
04-02 20:47:58.475: W/System.err(699): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 20:47:58.475: W/System.err(699): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 20:47:58.475: W/System.err(699): at java.lang.reflect.Method.invoke(Method.java:507)
04-02 20:47:58.482: W/System.err(699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 20:47:58.482: W/System.err(699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 20:47:58.482: W/System.err(699): at dalvik.system.NativeStart.main(Native Method)
04-02 20:47:58.482: W/System.err(699): Caused by: java.net.ConnectException: /127.0.0.2:80 - Connection refused
04-02 20:47:58.493: W/System.err(699): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
04-02 20:47:58.493: W/System.err(699): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
04-02 20:47:58.493: W/System.err(699): at java.net.Socket.connect(Socket.java:983)
04-02 20:47:58.493: W/System.err(699): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-02 20:47:58.493: W/System.err(699): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
04-02 20:47:58.493: W/System.err(699): ... 21 more
04-02 20:47:58.493: E/log-tag(699): error in the get data function
获取所有客户.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();
?>