Php 将android设备连接到SQLServer2005时出现JSON解析错误
我已经完成了以下教程: 用于使用php将android设备连接到sqlserver(2005)。我已经检查了我的php脚本,它运行和执行都很好。运行程序时,出现以下错误:Php 将android设备连接到SQLServer2005时出现JSON解析错误,php,android,json,sql-server-2005,Php,Android,Json,Sql Server 2005,我已经完成了以下教程: 用于使用php将android设备连接到sqlserver(2005)。我已经检查了我的php脚本,它运行和执行都很好。运行程序时,出现以下错误: 01-26 14:17:43.491: E/log_tag(331): Error parsing data org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONArray 01-27 09:2
01-26 14:17:43.491: E/log_tag(331): Error parsing data org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONArray
01-27 09:24:13.610: E/log_tag(404): Error parsing data org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONArray
01-27 09:26:45.190: E/log_tag(437): Error parsing data org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONArray
01-27 09:31:14.221: E/log_tag(471): Error parsing data org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONArray
01-27 09:43:44.501: E/log_tag(504): Error parsing data org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONArray
我希望我的程序连接到数据库,并返回EngineerId大于零的所有人的姓名。这是我的密码:
package com.david.DbConnect;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DbConnectActivity extends Activity {
/** Called when the activity is first created. */
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retreival
txt.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "http://xxx.xxx.xx.xx/dbconnect.php"; //i use my real ip here
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("EngID","0"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}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);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","ContactName: "+json_data.getInt("ContactName")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
package com.david.DbConnect;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类DbConnectActivity扩展了活动{
/**在首次创建活动时调用*/
文本视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个粗略的视图-这应该通过布局资源进行设置
//但是,因为这是一个示例,所以不需要在XML中声明它们。
LinearLayout rootLayout=新的LinearLayout(getApplicationContext());
txt=新文本视图(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
//设置文本并调用connect函数。
txt.setText(“连接…”);
//调用该方法以运行数据检索
setText(getServerData(KEY_121));
}
公共静态最终字符串键_121=”http://xxx.xxx.xx.xx/dbconnect.php“;//我在这里使用我的真实ip
私有字符串getServerData(字符串返回字符串){
InputStream=null;
字符串结果=”;
//要发送的年度数据
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“EngID”,“0”);
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(键号121);
//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;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);
对于(int i=0;i
有人能解释一下吗?它已经折磨了我好几天了。谢谢你用PHP输出了错误的数据。除了Json,你不能输出任何数据,所以你应该删除:
echo "Connection established.<br />";
此外,在发送任何数据输出之前,您应该为json添加标题:
header('Content-type: application/json');
我认为您从服务器获取的数据不是Json文件格式。您在第行有一个例外:
JSONArray jArray = new JSONArray(result);
当我取出回显数据时,我现在有一个连接错误:http connection org.apache.http.conn.HttpHostConnectException中的错误:连接被拒绝。有什么想法吗??
header('Content-type: application/json');
JSONArray jArray = new JSONArray(result);