android php发布
我目前正在开发一个android应用程序,它通过本地运行的php脚本检索mysql数据,但我一直收到一个httpconnection错误,该错误表示它无法连接到该脚本(http://127.0.0.1/regions.php)我正在本地服务器上运行。代码如下:android php发布,php,android,Php,Android,我目前正在开发一个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.a
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();
}
}
}
import org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入org.apache.*;
导入org.apache.http.*;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入android.app.Activity;
导入android.content.Context;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Window;
导入android.widget.*;
导入java.io.*;
导入java.util.ArrayList;
导入java.util.List;
导入com.paypal.android.MEP.*;
公共类ShoppingCartActivity扩展了活动{
杰索纳雷·贾雷;
List nameValuePairs=新的ArrayList();
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.main);
InputStream=null;
StringBuilder sb=null;
字符串结果=null;
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://127.0.0.1/regions.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();//它保存返回的内容
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH\u LONG.show();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析数据
国际注册号;
字符串注册表名;
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
对于(inti=0;i尝试使用10.0.2.2
而不是127.0.0.1
(在android代码中)
此处的说明:您正在尝试访问本地主机,但这是计算机的本地主机,而不是Android设备。如果您在真实设备上运行代码,则该设备需要与计算机位于同一网络中,并且您应该将主机更改为计算机的网络ip,而不是本地主机的ip(127.0.0.1
)
如果您在emulator上运行,那么正如@enrmarc所写,您可以尝试使用这个特殊的IP访问桌面的本地主机
127.0.0.1
->10.0.2.2
在android代码中,HttpPost(“http://127.0.0.1/regions.php“”;
可以是INVIILD
因为开发机器和测试设备的本地主机不同
您可以将开发机器和测试设备连接到网络,并获取要测试的开发机器的网络地址
或者将php scirpt上传到免费主机进行测试
快乐编码:D
<?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);
?>