我试图使用PHP脚本从mysql检索数据,但获取错误为错误解析Dataorg.json.JSONException

时间:2014-03-05 07:10:05

标签: php android mysql json

我正在尝试使用php脚本和JSON响应从mysql数据库中检索数据。这是我的MainActivity.java文件。当我运行此代码时,活动打开但没有显示任何内容。在日志文件中我得到错误为错误解析Dataorg.json.JSONException:Value

    package com.example.mysqlconnection;
    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.util.Log;
    import android.widget.TextView;
    import android.app.Activity;

       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();
   }

  public void getData(){

    String result = "";
    InputStream isr = null;
     try{

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.1.65/sheetal         /getdata.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("Could not Connect to database");
    }
      //Convert response to string

    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){
        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++){
            JSONObject json = jArray.getJSONObject(i);
             s = s+
                    "ISBN:" +json.getInt("ISBN")+ "\n"+
                    "Title:" +json.getString("Title")+ "\n"+
                    "Author:" +json.getString("Author")+"\n"+
                    "Edition:" +json.getInt("Edition")+"\n\n";
        }
        resultView.setText(s);

    }
    catch(Exception e){
        //TODO: handle Exception
        Log.e("log-tag","Error Parsing Data"+e.toString());
    }

     }
     }

这是JSON数据

[
    {"ISBN":"1","Title":"Basic Algorithm","Author":"XYZ","Edition":"2"},
    {"ISBN":"2","Title":"Operating System","Author":"ABC","Edition":"1"},
    {"ISBN":"3","Title":"System Software","Author":"WWW","Edition":"3"}
]

这是我的logcat

    03-11 00:54:56.059: E/log_tag(1451): Error in http      connectionorg.apache.http.conn.HttpHostConnectException: Connection to  https://192.168.1.64 refused
    03-11 00:54:56.079: E/log_tag(1451): Error converting resultjava.lang.NullPointerException: lock == null
    03-11 00:54:56.089: E/log-teg(1451): Error Parsing Dataorg.json.JSONException: End of input at character 1 of  

0 个答案:

没有答案