0

This is the code that doesnt work. I found it online and i try to use it myself but the code of HTTP is always with an strok trough it and it doesnt work. i think its because its out dated but i have searched all over the internet and couldnt found an answer so i thought i might ask it here.

private class BackTask extends AsyncTask<Void,Void,Void> { ArrayList<String> list; protected void onPreExecute(){ super.onPreExecute(); list=new ArrayList<>(); } protected Void doInBackground(Void...params){ InputStream is=null; String result=""; try{ HttpClient httpclient=new DefaultHttpClient(); HttpPost httppost= new HttpPost("http://10.0.2.2:8080/getInterviewees.php"); HttpResponse response=httpclient.execute(httppost); HttpEntity entity = response.getEntity(); // Get our response as a String. is = entity.getContent(); }catch(IOException e){ e.printStackTrace(); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8")); String line = null; while ((line = reader.readLine()) != null) { result+=line; } is.close(); //result=sb.toString(); }catch(Exception e){ e.printStackTrace(); } // parse json data try{ JSONArray jArray =new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject jsonObject=jArray.getJSONObject(i); // add interviewee name to arraylist list.add(jsonObject.getString("iname")); } } catch(JSONException e){ e.printStackTrace(); } return null; } protected void onPostExecute(Void result){ listItems.addAll(list); adapter.notifyDataSetChanged(); } } 
1

3 Answers 3

1
android { compileSdkVersion ... buildToolsVersion ... useLibrary 'org.apache.http.legacy' defaultConfig { ... } 

Use this on your build.gradle for app.

Sign up to request clarification or add additional context in comments.

Comments

1

Try adding this in your gradle file.

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

Comments

1

Or you can simply use httpUrlConnection instead of that...

1 Comment

yes i know but i wouldnt know how to do it, can you make an example?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.