-5

I am having some problems with my code. I have to read using JSONParser and I get this underlining on my code and it says that the problem is that i need try/catch clause but when I add the whole code is underlined and it says I have to do something with my api code.

Here is my code:

 public void onResponse(JSONObject response){
                            JSONArray quest=response.getJSONArray("students");
                            for(int i=0;i<quest.length();i++){
                                JSONObject question=quest.getJSONObject(i);
                                String name=question.getString("name");
                                String q=question.getString("question");


                                label.append(name+" asked "+ q);
                            }
                        }

Thank you!

John Miller
  • 3
  • 1
  • 4
  • What is the problem? – Kenneth Dec 23 '15 at 12:10
  • @JohnMiller The classes which you are using here are already **deprecated**, so don't use them. – Nigam Patro Dec 23 '15 at 12:11
  • Actually the HttpClient and HttpPost are deprecated and in API level 23 It was removed. that's why you got warnings in Android studio change your network connection to HttpURLConnection instead of HttpClient. – Ravi Jaggarapu Dec 23 '15 at 12:12
  • Possible duplicate of [Calling REST API from an android app](http://stackoverflow.com/questions/29339565/calling-rest-api-from-an-android-app) – Nigam Patro Dec 23 '15 at 12:13
  • http://stackoverflow.com/questions/34288737/cannot-resolve-symbol-defaulthttpclient-httpget-and-httppost-in-android-studio Show for ref. – Chirag Savsani Dec 23 '15 at 12:14
  • Huh, the *edit* is **not a means** to ask a **new, totally different** question (that is about your edit 3) – John_West Dec 27 '15 at 12:20

3 Answers3

0

It is suggested to use HttpUrlConnection instead, since HttpClient is deprecated. For further research; http://developer.android.com/reference/java/net/HttpURLConnection.html

dgngulcan
  • 3,101
  • 1
  • 24
  • 26
0
 void hitService() {

        String resp = "";

        HttpClient client = new DefaultHttpClient();
        String postURL = serviceUrl;

        HttpPost post = new HttpPost(postURL);


            HttpResponse response = client.execute(post);
            // Convert the response into a String
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {

                resp = EntityUtils.toString(resEntity);
                Log.i(TAG, resp);
            }
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        } catch (ClientProtocolException cpe) {
            cpe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return resp;

    }
Ayush
  • 173
  • 1
  • 1
  • 12
0

HttpClient and HttpPost are deprecated and in API level 23 It was removed. that's why you got warnings in Android studio change your network connection to HttpURLConnection instead of HttpClient.

For implementation of HttpURLConnection check the guide lines

Even though you need to use HttpClient with target SDK =>23 Android gives a support with library for that you need to add following line to your build.gradel

 android{
useLibrary 'org.apache.http.legacy'}

Note: They are giving clear documentation of causes for removing HttpClient from Android and they are providing support and implementation of new Network connection here

Ravi Jaggarapu
  • 627
  • 3
  • 10