这是MainActivty.java
中的我的代码。在运行程序时,我也添加了Internet权限,但我只是输出错误Occured!
我想显示从网址获取的数据。 Plz帮助我!!
MainActivty.java
public class MainActivity extends AppCompatActivity {
Button button;
TextView textView;
RequestQueue requestQueue;
String data="";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.HitButt);
textView = (TextView) findViewById(R.id.TV);
requestQueue = Volley.newRequestQueue(this);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
try
{
response = response.getJSONObject("movies");
String first = response.getString("movie");
String last = response.getString("year");
textView.append(first + " " + last + "\n");
Log.d("RESPONSE", response.toString());
}catch (JSONException e1) {
e1.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
textView.setText("Error Occured!!");
}//void.onErrorResponse
}//Response.ErrorListener
);//jsonObjectRequest
requestQueue.add(jsonObjectRequest);
}//view.onClick
}//view.OnClickListener
);//btn.onClick
} //的onCreate
} //主
答案 0 :(得分:0)
response = response.getJSONObject("movies");
虽然你有一个对象数组
"movies": [ { "movie": "Avengers", "year": 2012 } ]
所以你必须像
那样做response = response.getJSONArray("movies").getJSONObject(0);