android Json无法使用volley从webservice获取数据

时间:2016-02-16 06:07:46

标签: android json android-volley

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

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

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String url = "http://oilpeople.talenetic.com/api/SearchJob?limit=5&jobkeyword=oil&countrytext=UnitedKingdom&location=London[GreaterLondon]&apikey=1111111111&siteid=1";

        JsonObjectRequest jsonRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // the response is already constructed as a JSONObject!
                        try {
                            JSONArray array = response.getJSONArray("joblist");
                            String name = array.getString(1),
                                    network = array.getString(2);
                            String[] myList ={name,network};
                            ListView mlist = (ListView) findViewById(R.id.jsonlist);

                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,myList);
                            mlist.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });

        Volley.newRequestQueue(this).add(jsonRequest);


    }


}

我正在尝试从此链接中获取json数据

http://oilpeople.talenetic.com/api/SearchJob?limit=5&jobkeyword=oil&countrytext=United Kingdom&location=London [Greater London]&apikey=1111111111&siteid=1

一切正常,但我无法显示获取的数据!我想知道我是否以正确的方式取出它。所以请浏览链接并检查我是否正确获取数据。 THANKYOU。

3 个答案:

答案 0 :(得分:0)

使用optJSONArray而不是getJSONArray,如果该键具有任何值,它将返回值,否则它将不会影响任何流。

通过使用它,您可以避免空指针异常

try {
    if(response !=null) { //check jsonObject is null or not
        JSONArray array = response.optJSONArray("joblist");
        String name = array.optString("jobid"),
                network = array.optString("jobtitle");
        String[] myList = {name, network};
        ListView mlist = (ListView) findViewById(R.id.jsonlist);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myList);
        mlist.setAdapter(adapter);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

我认为以下是错误。 一个额外的&#39;对象将进入响应。

if(response!=null && response.length>0){
JSONObject objmain = new JSONObject(response.getString("d"));
JSONArray array = response.getJSONArray("joblist");
//your required code here//
//it will work//
}

答案 2 :(得分:0)

如果您有多个对象,请使用此

for(int i = 0; i < response.length(); i++){
JSONObject jresponse = response.getJSONObject(i);
String stringValue= jresponse.getString("string_key);
Log.d("string_key", stringValue);

}

如果您只有一个对象,请使用以下代码

try {
    JSONObject jresponse = response.getJSONObject(0);
    String stringValue= jresponse.getString("string_key");
    Log.d("string_key",stringValue);
}catch (JSONException e) {
    e.printStackTrace();
}
相关问题