无法在Android中将java.lang.String类型的值转换为JSONArray
我无法找到此错误的解决方案 这是我的错误无法在Android中将java.lang.String类型的值转换为JSONArray,java,android,json,Java,Android,Json,我无法找到此错误的解决方案 这是我的错误 org.json.JSONException:Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray java类代码 entpackage com.example.blood; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamRead
org.json.JSONException:Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
java类代码
entpackage com.example.blood;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
resultView=(TextView) findViewById(R.id.result);
getData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void getData()
{
String result="";
InputStream isr=null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://10.0.2.2/blood/index.php");
httppost.setHeader("Content-Type", "application/json");
httppost.setHeader("Accept", "JSON");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
isr=entity.getContent();
}catch(Exception e){
String err=e.toString();
resultView.setText("Could not connect to database"+err);
Log.e("error", err);
}
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(Exception e){
String err2=e.toString();
resultView.setText("Error coverting result"+err2);
Log.e("error", err2);
}
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("name")+"\n"+
"Blood Group :"+json.getString("bgroup")+"\n" +
"Address :"+json.getString("address")+"\n\n";
}
resultView.setText(s);
}catch(Exception e){
String err1=e.toString();
resultView.setText("xxx"+err1);
Log.e("error", err1);
}
}
}
当我运行应用程序时,它显示字符串不能转换为JSONArray。我还在本地主机上工作。现在我该如何解决这个问题。提前感谢不知怎的,我感觉你的index.php并没有按照你的想法返回你的json。您可以在解析之前先打印出它的值吗?php代码:我在解析之前打印值您的响应字符串开始时,我使用http连接从服务器接收到几乎相同的值。我所做的不是读取以
entpackage com.example.blood;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
resultView=(TextView) findViewById(R.id.result);
getData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void getData()
{
String result="";
InputStream isr=null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://10.0.2.2/blood/index.php");
httppost.setHeader("Content-Type", "application/json");
httppost.setHeader("Accept", "JSON");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
isr=entity.getContent();
}catch(Exception e){
String err=e.toString();
resultView.setText("Could not connect to database"+err);
Log.e("error", err);
}
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(Exception e){
String err2=e.toString();
resultView.setText("Error coverting result"+err2);
Log.e("error", err2);
}
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("name")+"\n"+
"Blood Group :"+json.getString("bgroup")+"\n" +
"Address :"+json.getString("address")+"\n\n";
}
resultView.setText(s);
}catch(Exception e){
String err1=e.toString();
resultView.setText("xxx"+err1);
Log.e("error", err1);
}
}
}