我正在开发一个Android应用程序,它通过我在本地运行的php脚本检索mysql数据,但我一直收到一个httpconnection错误,指出它无法连接到脚本(http://127.0。 0.1 / regions.php)我正在我的本地服务器上运行。代码如下:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.*;
import org.apache.http.*;
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 android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import com.paypal.android.MEP.*;
public class ShoppingCartActivity extends Activity {
JSONArray jArray;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
InputStream is = null;
StringBuilder sb = null;
String result = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://127.0.0.1/regions.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();//this holds returned content
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//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="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parsing data
int reg_id;
String reg_name;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
reg_id=json_data.getInt("RegionID");
reg_name=json_data.getString("RegionName");
}
}catch(JSONException e1){
Toast.makeText(getBaseContext(), "No Regions Found", Toast.LENGTH_LONG).show();
}catch (ParseException e1){
e1.printStackTrace();
}
}
}
这是我发布的php脚本:
<?php
$conn = mysql_connect("127.0.0.1","","");
mysql_select_db("thebigchoice") or die(mysql_error());
$query = mysql_query("SELECT * FROM REGIONS WHERE RegionName LIKE 's%'", $conn) or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
$data[] = $row;
}
print json_encode($data);
mysql_close($conn);
?>
答案 0 :(得分:0)
尝试使用10.0.2.2
代替127.0.0.1
(在Android代码中)
这里有解释:Network addresses
答案 1 :(得分:0)
您正在尝试访问本地主机,但这是计算机的本地主机,而不是Android设备。如果您在真实设备上运行代码,则设备需要与计算机位于同一网络中,您应将主机更改为计算机的网络IP,而不是ip for localhost(127.0.0.1
)
如果你在模拟器上运行,那么就像@enrmarc写的那样,你可以尝试使用这个特殊的IP来访问桌面的本地主机。
127.0.0.1
- &gt; 10.0.2.2
答案 2 :(得分:0)
在Android代码中,HttpPost("http://127.0.0.1/regions.php");
可能是invaild
因为开发机器和测试设备的本地主机是不同的。
您可以将开发机器和测试设备连接到网络并获取开发机器的网络地址进行测试
或者将php scirpt上传到免费主机进行测试
快乐编码:D