2

I am unable to parse JSON data ,it always give me [] whenever i am parsing i have checked on jsonlint.com for valid json ,it returns the correct data

url is http://egravity.in/csvupload/hello.php/ and in params i am using param.add(new BasicNameValuePair("id", "14")); thus i expect url to be=http://egravity.in/csvupload/hello.php/?id=14

package com.example.prototype.utility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;

@SuppressWarnings({ "deprecation", "unused" })
public class JSONUtil {

    JSONObject result;
    String temp;
    String t;
    InputStream is;
    String exception="";
    static String json = "";
    static JSONObject jObj = null;

  public String setConnection(String url,String method,List <NameValuePair>params){
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        DefaultHttpClient client=new DefaultHttpClient();
        if(method.equalsIgnoreCase("post")){



            HttpResponse httpResponse = null;

            try {

                HttpPost httpPost = new HttpPost(url);
               httpPost.setEntity(new UrlEncodedFormEntity(params));

                t= httpPost.getURI().toString();
    Log.e("ur", t);
                httpResponse = client.execute(httpPost);
                Log.e("response",httpResponse.toString());
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                Log.e("content_bestfrag ","input stream have "+is.available()  );

        if(is==null)
                Log.e("content ","input stream is null");

        else
            Log.e("content_bestfrag ","input stream is not null "+is.available()  );



            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                exception+=e.getMessage()+"  ";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                exception+=e.getMessage()+"  ";

            }

        }


        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is));


            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

            }catch(Exception e){
                Log.e("Buffer Error", "Error converting result " + e.getMessage());
            }

            Log.e("json", json);
            return json;
    }





}

Whenever i print json(a string containg data returned) it shows [] instead of data please help me...!!!

5
  • Are u sure you are sending params. Commented Aug 10, 2015 at 12:18
  • can you see your logcat Log.e("ur", t); what's it's value? Commented Aug 10, 2015 at 12:19
  • yes i am sending params Commented Aug 10, 2015 at 12:19
  • yes bt it is not adding params to it ,it is like 08-10 17:51:27.149: E/ur(21896): egravity.in/csvupload/hello.php Commented Aug 10, 2015 at 12:21
  • For parsing use ''Gson'' lib Commented Aug 10, 2015 at 12:22

3 Answers 3

1

You should make a get request rather than post try this code

    HttpClient client = new DefaultHttpClient();


    URI website = new URI("http://egravity.in/csvupload/hello.php/?id=14"); 
    HttpGet request = new HttpGet();
    request.setURI(website);
    HttpResponse response = client.execute(request);
    response.getStatusLine().getStatusCode();

    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer sb = new StringBuffer("");
    String l = "";
    String nl = System.getProperty("line.separator");
    while ((l = in.readLine()) != null) {
        sb.append(l + nl);
    }
    in.close();
    data = sb.toString();
Sign up to request clarification or add additional context in comments.

3 Comments

how will i set dynamically passed params like ,we set in post
You can use stringbuffer to append the paramter to your url
Example: String paramString = URLEncodedUtils.format(params, "utf-8"); StringBuffer stringBuffer = new StringBuffer(URL); stringBuffer .append("?"); stringBuffer .append(paramString); final URL as String stringBuffer .toString()
0

You use HttpPost, but in url like "http://egravity.in/csvupload/hello.php?id=14" - HttpGet with param id=14

Comments

0

EntityUtils.toString() is easy to gets HttpResponse string.

HttpPost post = new HttpPost(url);
HttpClient client = new DefaultHttpClient();

post.setEntity(new UrlEncodedFormEntity(params1, charset));
HttpResponse resp = client.execute(post);
String strResp = EntityUtils.toString(resp.getEntity());

JSONObject jsonResp = new JSONObject(strResp);
System.out.println(jsonResp.toString(4));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.