Reputation: 79
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.clcik);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List list = new ArrayList<>();
String url ="http://abcd.abcd.com/api/SearchJob? limit=5&jobkeyword=oil&countrytext=United%20Kingdom&location=London%20[Greater%2 0London]&apikey=1111111111&siteid=1";
HttpPost httppost = new HttpPost(url);
try{
HttpClient client = new DefaultHttpClient() ;
HttpResponse response = client.execute(httppost);
InputStream is = response.getEntity().getContent();
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();
String json = sb.toString();
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("jobslist");
for(int i=0;i<jsonArray.length();i++){
JSONObject c = jsonArray.getJSONObject(i);
String jobtitle = c.getString("jobtitle");
list.add(jobtitle);
Log.d("list", jobtitle);
}
ArrayAdapter<List> adapter = new ArrayAdapter<List>(getApplicationContext(),android.R.layout.simple_list_item_1,list);
ListView listView = (ListView) findViewById(R.id.jsonlist);
listView.setAdapter(adapter);
}catch(Exception e){
}
}
});
}
}
This is my MainActivity code. i am trying to fetch data from a webservice but i am not able to display it .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:id="@+id/clcik"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jsonlist"
></ListView>
</LinearLayout>
This my xml code.
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.practice.android.json"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
This is my manifest file .Can someone Please Help me out.Forget about the url its just an example. is something wrong with my code?? i am using sdk 23 and the Http Methods are decripted but i have seen people tell that it still works.
Upvotes: 0
Views: 57
Reputation: 646
your trying to make a web request on the UIThread which in android is not allowed.
you need to make any web requests in a separate thread. Looking at your code you are using the data in the web request to build up a ListView.
take a look at the following link to see how you can do this.
Upvotes: 1