使用烂番茄 API 时出现以下错误:
org.json.JSONException:java.lang.String 类型的值 596 无法转换为 JSONArray。
我正在尝试从 Rotten Tomatoes API 中解析出 JSON 格式的数据。
这是我的代码的样子。
package com.my.apitest;
import java.io.*;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.json.*;
import org.apache.http.impl.client.*;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText display;
HttpResponse responseGet;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (EditText) findViewById(R.id.textBox);
String result = "";
try {
HttpClient client = new DefaultHttpClient();
String movieID = "770672123";
HttpPost getCast = new HttpPost("http://api.rottentomatoes.com/api/public/v1.0/movies/" +
movieID + "/cast.json?apikey=3p9ehnhzbxwpbd6mk8fncf67");
HttpResponse responseGet = client.execute(getCast);
HttpEntity resEntityGet = responseGet.getEntity();
InputStream webs = resEntityGet.getContent();
// (i was converting to string like this)display.setText(EntityUtils.toString(resEntityGet));
try{
BufferedReader reader = new BufferedReader (new InputStreamReader (webs, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
webs.close();
result=sb.toString();
}catch(Exception e
Log.e("log_tag", "Error converting result: " + e.toString());
}
}catch(Exception e
Log.e("log_tag", "Error in http connection " + e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for (int i=0; i<jArray.length(); i++
JSONObject json_data = jArray.getJSONObject(i);
display.setText(json_data.getString("name"));
}
}
catch(JSONException e
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
catch (Exception e
Log.e("Error", "Error in Code:" + e.toString());
e.printStackTrace();}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return false;
}
}
在进行了一些挖掘之后,我遇到了一些信息建议,我需要使用 PHP 连接到 api。我是 API 和移动开发的新手,所以我不确定如何去做,或者这是否真的是问题所在。我认为可能发生的情况是由于没有 PHP 文件连接到 API 和解码 JSON 而引发错误。听起来对吗?任何帮助将不胜感激。