从服务器获取数据的JSONException

时间:2014-08-13 10:11:01

标签: php android json

我'从在线数据库获取数据,我没有连接问题或类似的东西,但我总是抓住这种情况下的这种情况:

  • if (!result.contains("null")){...}

                    JSONArray jArray = new JSONArray(result);
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject job = jArray.getJSONObject(i);
    
                    }
    
                } catch (JSONException e) {
                    Log.e("Parser Problem", e.toString() + result);
                }
            } 
    

我的logcat向我展示了Log.e(...)

 E/Parser Problem(7300): org.json.JSONException: Value <script of type java.lang.String cannot be converted to JSONArray

这是我的php脚本,我的服务器内有一个查询:

<?php
$query = "SELECT name
FROM users
HAVING name = John";                    
$result = $connection->query($query);
    while($row = $result->fetch(PDO::FETCH_ASSOC)) {
            $output[]=$row;
    }
    echo json_encode($output);
    $connection = null;
?>

非常感谢。

2 个答案:

答案 0 :(得分:1)

当您使用JSON时,您必须确保您的网络文件中包含的输出必须位于JSON ..

输出中有HTML部分.. 所以它无法转换为JSON ..

PHP文件存在问题 您的 PHP文件包含HTML表单 ..删除它并运行您的代码...

答案 1 :(得分:0)

proxy for post method:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;
import com.youflik.youflik.utils.Util;
import android.util.Log;


public class HttpPostClient {
    private static final String TAG = "HttpClient";
    public static int statuscode;

    public static JSONObject sendHttpPost(String URL, JSONObject jsonObjSend) {

        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();


            HttpPost httpPostRequest = new HttpPost(URL);

            StringEntity se;

            se = new StringEntity(jsonObjSend.toString());

            // Set HTTP parameters
            httpPostRequest.setEntity(se);
            httpPostRequest.setHeader("Content-type", "application/json");
            httpPostRequest.setHeader("Accept-Charset", "utf-8");


            long t = System.currentTimeMillis();
            HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
            Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");


            StatusLine statusline = response.getStatusLine();
            statuscode = statusline.getStatusCode();


            // Get hold of the response entity (-> the data):
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                // Read the content stream
                InputStream instream = entity.getContent();

                // convert content stream to a String

                try {

                    String resultString= convertStreamToString(instream);
                    instream.close();
                    // Transform the String into a JSONObject
                    JSONObject jsonObjRecv = new JSONObject(resultString);

                    return jsonObjRecv;

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } 

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("converted string is "+sb.toString());
        return sb.toString();
    }



}
proxy for get method
public class HttpGetClient {
    public static int statuscode;

    public static JSONObject sendHttpPost(String URL){

        try {
            HttpClient httpclient;
            httpclient = HttpClientSingalTon.getHttpClienttest();
            HttpGet httpGetRequest = new HttpGet(URL);
            if(Util.API_TOKEN !=null)
            {
                System.out.println(Util.API_TOKEN);

            }

            HttpResponse response;

            //  System.out.println("executing request " + httpGetRequest.getRequestLine());
            //  System.out.println("executing request " + httpGetRequest.getMethod());
            //  System.out.println("executing request " + httpGetRequest.getFirstHeader("X-Auth-Token"));
            //  System.out.println("executing request Tsting  " + httpGetRequest.getHeaders("X-Auth-Token"));

            response = httpclient.execute(httpGetRequest);

            StatusLine statusline = response.getStatusLine();
            statuscode = statusline.getStatusCode();
            // Examine the response status
            Log.i("Praeda",response.getStatusLine().toString());

            // Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();

                String result= convertStreamToString(instream);

                // now you have the string representation of the HTML request
                instream.close();
                JSONObject jsonObjRecv = new JSONObject(result);

                return jsonObjRecv;

            }


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(sb.toString());
        return sb.toString();
    }       


}