Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON Data from URL Using Android?

Tags:

I am trying to get JSON data by parsing login url with username and password. I have tried by using below code but I can't get any responses. Please help me.

I am using HTTP Process and API level 23.

I need to parse my URL and get below Response

{     "response":{                 "Team":"A",                 "Name":"Sonu",                 "Class":"First",               },                 "Result":"Good", } 

Below My code :

public class LoginActivity extends Activity {      JSONParser jsonparser = new JSONParser();     TextView tv;     String ab;     JSONObject jobj = null;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.login);          new retrievedata().execute();      }      class retrievedata extends AsyncTask<String, String, String>{          @Override         protected String doInBackground(String... arg0) {             // TODO Auto-generated method stub             jobj = jsonparser.makeHttpRequest("http://myurlhere.com");              // check your log for json response             Log.d("Login attempt", jobj.toString());              try {                 ab = jobj.getString("title");             } catch (JSONException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }             return ab;         }         protected void onPostExecute(String ab){              tv.setText(ab);         }      }  } 
like image 857
android Avatar asked Oct 20 '15 06:10

android


People also ask

What is JSON object in Android?

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it. Android provides four different classes to manipulate JSON data.


1 Answers

Easy way to get JSON especially for Android SDK 23:

public class MainActivity extends AppCompatActivity {  Button btnHit; TextView txtJson; ProgressDialog pd;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      btnHit = (Button) findViewById(R.id.btnHit);     txtJson = (TextView) findViewById(R.id.tvJsonItem);      btnHit.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             new JsonTask().execute("Url address here");         }     });   }   private class JsonTask extends AsyncTask<String, String, String> {      protected void onPreExecute() {         super.onPreExecute();          pd = new ProgressDialog(MainActivity.this);         pd.setMessage("Please wait");         pd.setCancelable(false);         pd.show();     }      protected String doInBackground(String... params) {           HttpURLConnection connection = null;         BufferedReader reader = null;          try {             URL url = new URL(params[0]);             connection = (HttpURLConnection) url.openConnection();             connection.connect();               InputStream stream = connection.getInputStream();              reader = new BufferedReader(new InputStreamReader(stream));              StringBuffer buffer = new StringBuffer();             String line = "";              while ((line = reader.readLine()) != null) {                 buffer.append(line+"\n");                 Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)               }              return buffer.toString();           } catch (MalformedURLException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         } finally {             if (connection != null) {                 connection.disconnect();             }             try {                 if (reader != null) {                     reader.close();                 }             } catch (IOException e) {                 e.printStackTrace();             }         }         return null;     }      @Override     protected void onPostExecute(String result) {         super.onPostExecute(result);         if (pd.isShowing()){             pd.dismiss();         }         txtJson.setText(result);     } } } 
like image 79
aadigurung Avatar answered Oct 03 '22 16:10

aadigurung