0

I connect a database on my server from the Android application and I want to use the info the server send back to me. Here is the response I get from the server :

[{"id":"1","userid":"1159448580","address":"Kishinev 3"}]

Here is my function that is realizing the connection to the server and trying to parse the response :

String checkForUserIdGetBookmarks (String returnString) {
        InputStream is = null;
        String result = "";
        //the userId data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("userId", userId));

        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(PHP_URL);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response to string
        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();
            result = sb.toString();
        } catch(Exception e) {
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        //parse json data
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i=0; i<jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","id: "+json_data.getInt("id")+
                        ", userid: "+json_data.getString("userid")+
                        ", address: "+json_data.getInt("address")
                        );
                //Get an output to the screen
                returnString += "\n\t" + jArray.getJSONObject(i);
            }
        } catch(JSONException e) {
            Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return returnString;
    }

But when I try to show the already converted string it shows an empty string.

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            String returnString = "";
        checkForUserIdGetBookmarks(returnString);
        Toast useridToast = Toast.makeText(getApplicationContext(), returnString, Toast.LENGTH_LONG);
        useridToast.show();

Where would you suggest is my mistake and do you know other way of getting the response from the server into a string which I can use afterwards?

3
  • String returnString = ""; checkForUserIdGetBookmarks(returnString); this seems to be the error. You could change it to String returnString = checkForUserIdGetBookmarks(); or am i missing something? Commented Aug 1, 2011 at 13:54
  • My function checkForUserIdGetBookmarks(String string) requires a String to work properly. Even if I use this: returnString = checkForUserIdGetBookmarks(returnString); I still don't get a persed string but an empty one instead. Commented Aug 1, 2011 at 13:58
  • I was trying to say that you could change your function. The input string you use in your function doesn't seem an input to the function. Commented Aug 1, 2011 at 14:14

1 Answer 1

2

Use:

String returnString = checkForUserIdGetBookmarks(); // and declare returnString inside the method

Instead of:

String returnString = "";
checkForUserIdGetBookmarks(returnString);
Sign up to request clarification or add additional context in comments.

4 Comments

If I use only String returnString = checkForUserIdGetBookmarks(returnString); if puts out an error - The local variable returnString may not have been initialized - If I use it as : String returnString = ""; returnString = checkForUserIdGetBookmarks(returnString); Nothing happens - I still receive and empty toast.
Changed code to : String returnString = checkForUserIdGetBookmarks(); Toast useridToast = Toast.makeText(getApplicationContext(), returnString, Toast.LENGTH_LONG); useridToast.show(); And in the function : String checkForUserIdGetBookmarks() { InputStream is = null; String result = ""; String returnString = null; .... returnString += "\n\t" + jArray.getJSONObject(i); But still an empty toast - is the parsing function okay?
Is it generating errors? I mean, what do you see when running logcat?
It was generating an error... stupid me - I entered a string for the address and was expecting an int for response... thanks for the help!

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.