0

Here is my java code. I want to show login to another page. JSOn SJon

public void jsonen()
{
 String result = response.toString();

 try{

 JSONArray jArray = new JSONArray(result);

}
    catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }



    Log.i("RESULT", result+"");
7
  • In which line you are getting error? Show the line. Commented Nov 29, 2013 at 6:26
  • 11-29 13:16:02.860: E/log_tag(648): Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject Commented Nov 29, 2013 at 6:50
  • I am asking in which line of your java code? If you dont know, please provide full logcat. Commented Nov 29, 2013 at 6:51
  • There are too long to show logcat in this box. What should I do. Commented Nov 29, 2013 at 7:00
  • Show only the re color errors over here. Commented Nov 29, 2013 at 7:00

3 Answers 3

2

as in log you are getting JSONObject instead of JSONArray in webservice response.so try to convert string to JSONObject instead of JSONArray as:

    JSONObject jsonobj = new JSONObject(result);
     //get value from json object
    teamStatus = jsonobj.getString("teamStatus");
Sign up to request clarification or add additional context in comments.

2 Comments

11-29 12:46:34.219: E/log_tag(986): Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
@user3044615 : please update your latest code and logs in question to get more help
0

For parsing and getting data form a JSON Us this:

Add a JsonParser.java in your src. And java file will be like this:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }
    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    } 
}

And Now add UserFunction.Java file in src. And it will be like this:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

public class UserFunctions {

    private JSONParser jsonParser;

    private static String loginURL = YOUR_URL;

public UserFunctions() {
        jsonParser = new JSONParser();
    }
public JSONObject loginUser(String email, String password) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
}

And your login.java will be:

String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
//do your staff
}catch{
}

8 Comments

Just let me know if it doesn't help you.
CustomHttpClient.executeHttpGet and CustomHttpClient.executeHttpPost are String return type. I can't change JSONObject. some red line will appear...
What is 88 and 191 line in login.java file?
String str = CustomHttpClient.executeHttpPost("10.0.2.2/football365/responseJson.php",pp); // Line 88 if(edit_txt_Password.getText().toString().equals("")||edit_txt_Password.getText().toString().trim().equals(null)) { showAlertDialog(Login.this, "Password", "Enter Password", false); edit_txt_Password.requestFocus(); } // line 190,191,192,193
May I know you can or not?
|
0

I have the same problem and fix it. Check your php files, especially print("") before conversion on JSON.

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.