来自响应的TextView

时间:2018-10-24 11:15:48

标签: java android json

我正在尝试从此API http://ip-api.com/json/?fields=query,country,city获取带有URL的地理位置JSON数据,并在我的TextView布局中显示IP地址,城市,国家/地区

响应:

{"city":"Bandung","country":"Indonesia","query":"127.0.0.1"}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    Button start;
    TextView textView;
    RequestQueue requestQueue;

    public static TextView data;

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

        start = (Button) findViewById(R.id.lokasi);
        textView = (TextView) findViewById(R.id.textViewLok);
        requestQueue = Volley.newRequestQueue(this);

        start.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View ){

               JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://ip-api.com/json/?fields=query,country,city",
                       new Response.Listener<JSONObject>() {
                           @Override
                           public void onResponse(JSONObject response) {

                               JSONObject jsonObject = new JSONObject(response);
                               String city = jsonObject.getString("city");
                               String country= jsonObject.getString("country");
                               String ip = jsonObject.getString("query");

                           }
                       },

                       new Response.ErrorListener() {
                           @Override
                           public void onErrorResponse(VolleyError error) {
                                Log.e("VOLLEY","ERROR");
                           }
                       }


               )
           }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您需要将您的响应(从api获取)放入JSONObject。

JSONObject jsonObject = new JSONObject(myResponse);

之后,您可以解析jsonObject。

String city = jsonObject.getString("city");
String country= jsonObject.getString("country");
String ip = jsonObject.getString("query");

然后将字符串放入文本视图中。

更新:

只需将onResponse更改为此。

@Override
public void onResponse(JSONObject response) {
       String city = response.getString("city");
       String country= response.getString("country");
       String ip = response.getString("query");
}

答案 1 :(得分:0)

第1步。为您的响应创建一个POJO,例如Example.java

    public class Example {

    @SerializedName("city")
    @Expose
    private String city;
    @SerializedName("country")
    @Expose
    private String country;
    @SerializedName("query")
    @Expose
    private String query;

    public String getCity() {
    return city;
    }

    public void setCity(String city) {
    this.city = city;
    }

    public String getCountry() {
    return country;
    }

    public void setCountry(String country) {
    this.country = country;
    }

    public String getQuery() {
    return query;
    }

    public void setQuery(String query) {
    this.query = query;
    }

    }

第2步。解析字符串响应

    Example ex = gson.fromJson(json, BoxSearch.class);

第3步。现在从POJO中获取价值

    String city = ex.getCity();
    String country= ex.getQuery();
    String ip = ex.getCountry();

第4步。在Textview中设置值

    txtCity.setText(city);
    txtCountry.setText(country);
    txtIp .setText(ip);