私はJSON解析に不慣れで、JSON URLからデータを取得するのに助けが必要です...JSON解析に関する多くのチュートリアルを読みましたが、それらはすべて私を大いに助けましたが、行き詰まりはありません。
URL = "http://mygogolfteetime.com/iphone/topfive/155"
しかし、私のログにはnull値がJson object
返されます。
ただし、別のデータから同じデータを取得できます
URL = "http://api.mygogolfteetime.com/top_5.php?id=155&apikey=eart3562@#bety7*&6b12zAf*&etrqvbn*&LMQW"
また、問題の解決に役立つようにコードを添付しています。
私のテスト活動のコード:
public class TestActivity extends Activity {
private static final String url = "http://mygogolfteetime.com/iphone/topfive/155";
private static final String TAG = "Top5";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "URL: "+String.valueOf(url));
JsonParser jParse = new JsonParser();
JSONObject json = jParse.getJSONfromUrl(url);
Log.v(TAG, "Json: "+String.valueOf(json));
}
}
これは私のJsonParserアクティビティのコードです:
public class JsonParser {
static InputStream is;
static JSONObject jObj;
static String json = null;
private static final String TAG = "Top5";
public JSONObject getJSONfromUrl(String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
try
{
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();
json = sb.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
jObj = new JSONObject(json);
}
catch(JSONException e)
{
e.printStackTrace();
}
return jObj;
}
}
マニフェストファイルにインターネットの許可も追加しました...
<uses-permission android:name="android.permission.INTERNET"/>
前もって感謝します...
英語が下手でごめんなさい。