分析数据org.json.JSONException时出错:java.lang.String类型的值无法转换为JSONArray
我正在从android应用程序连接到外部数据库。它是使用PHP JSON的MS SQL Server。 我已成功连接到数据库并在浏览器中获得结果。来到android时,我没有得到任何结果,它显示解析数据org.json.JSONException时出现分析数据org.json.JSONException时出错:java.lang.String类型的值无法转换为JSONArray,java,android,database,json,Java,Android,Database,Json,我正在从android应用程序连接到外部数据库。它是使用PHP JSON的MS SQL Server。 我已成功连接到数据库并在浏览器中获得结果。来到android时,我没有得到任何结果,它显示解析数据org.json.JSONException时出现错误:java.lang.String类型的值无法转换为JSONArray。下面是我的代码 getDatabase.php 数据库活动 public class Database extends Activity { /** Called wh
错误:java.lang.String类型的值无法转换为JSONArray
。下面是我的代码
getDatabase.php
数据库活动
public class Database extends Activity {
/** Called when the activity is first created. */
TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
StrictMode.enableDefaults(); // STRICT MODE ENABLED
resultView = (TextView) findViewById(R.id.result);
getData();
}
public void getData() {
String result = "";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.example.com/getDatabase.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
resultView.setText("Couldnt connect to database");
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
isr, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
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++) {
String json = jArray.getString(i); // JSONObject
// json = jArray.getJSONObject(i);
JSONObject jObj = new JSONObject(json);
s = s + "Name: " + jObj.getString("parkingtitle") + "\n\n"
+ "Address: " + jObj.getString("address") + "\n\n";
}
resultView.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data " + e.toString());
}
}
}
公共类数据库扩展活动{
/**在首次创建活动时调用*/
文本视图结果视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
StrictMode.enableDefaults();//已启用严格模式
resultView=(TextView)findViewById(R.id.result);
getData();
}
public void getData(){
字符串结果=”;
InputStream isr=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://www.example.com/getDatabase.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
isr=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
resultView.setText(“无法连接到数据库”);
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
isr,“utf-8”)、8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
isr.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
字符串s=“”;
JSONArray jArray=新JSONArray(结果);
for(int i=0;i
我在谷歌上搜索并查看了所有帖子,但没有找到任何解决方案。
请帮我做这个。
提前感谢。尝试使用:
JSONObject jObj = jArray.getJSONObject(i);
而不是:
jArray.getString(i); // JSONObject // json = jArray.getJSONObject(i);
JSONObject jObj = new JSONObject(json);
试试这个。JSONObject jobj=新JSONObject(结果);发布结果字符串。如果您发送的是格式良好的JSON,它将不仅仅是一个数组。它将是一个JSONObject,里面有一个JSONArray,需要像这样解析。嗨,selva,我用JSONObject jobj=new JSONObject(result)试过了,然后解析数据org.json.JSONException时出错:java.lang.String类型的值不能转换为JSONObject。结果的值是什么字符串?嗨,simon。我尝试使用您的解决方案,但得到的错误消息相同,无法转换为jsonarray。那么索引I处的对象不是JSONObject
jArray.getString(i); // JSONObject // json = jArray.getJSONObject(i);
JSONObject jObj = new JSONObject(json);