How to get json object in http connection

MainActivity.java


package android.com.getjson;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
TextView tv;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.text);
listView= (ListView) findViewById(R.id.list_item);
LoadUrl loadUrl=new LoadUrl();
loadUrl.execute();

}

class LoadUrl extends AsyncTask<String, Void, String>
{
String result="";
ArrayAdapter adapter;
@Override
protected String doInBackground(String... params) {

try {
URL url=new URL("enter your url here");
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.connect();

JSONObject jsonObject=new JSONObject();
jsonObject.put("TipsiteoperatorId", 53); //according to your post

OutputStreamWriter out=new OutputStreamWriter(urlConnection.getOutputStream());
out.write(jsonObject.toString());
out.close();


BufferedReader br=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

String line;
while((line=br.readLine())!=null)
{
result+=line;
}


JSONObject js=new JSONObject(result);

JSONArray jsonArray=js.getJSONArray("responsepacket");

String[] userDetail=new String[jsonArray.length()];
for(int i=0; i<jsonArray.length();i++)
{
JSONObject jsonObject1=jsonArray.getJSONObject(i);
userDetail[i]=String.valueOf(jsonObject1.getString("Id"));
userDetail[i]=userDetail[i]+" : "+jsonObject1.getString("ProductName");
}

adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.items,userDetail);






} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}


return null;
}

@Override
protected void onPostExecute(String s) {

listView.setAdapter(adapter);
super.onPostExecute(s);
}
}


}



Layouts

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="android.com.getjson.MainActivity">
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

items.xml

<TextView
android:id="@+id/items"
android:layout_margin="10dp"
android:textSize="30dp"
android:text="list item"
android:textColor="#000000"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />

Share on Google Plus

About Mayank Sharma

Mayadi is a knowlage source. You can learn, explore and play many things in a proper way. Mayadi Provides Best, Perfect and Quality Information. subscribe our youtube channel to be updated and and don't forget to take a look on our blog within every week, because we are here to upgrade your knowladge. We love to see you again and again and again...
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment