0
 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.

4
  • Possible duplicate of Json Parsing in Android Application Commented Feb 15, 2016 at 8:22
  • so what's the problem? are you getting errors? worst thing is using empty catch like this catch(Exception e){ } add e.printStacktrace(); and see what's going on Commented Feb 15, 2016 at 8:22
  • no i am not gettng any errors Commented Feb 15, 2016 at 8:50
  • use AsyncTask for communication between your server and mobile app, or any sort of network communicaiton. Commented Feb 15, 2016 at 9:33

1 Answer 1

1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.