这是我在Stackoverflow上的第一篇文章,我正在尝试学习Android。我需要从Android应用程序中读取一个json文件。
I ran the following code on my phone and it doesn't display anything on my phone,
I just get a blank screen.
我知道有很多方法可以使用Java库来解析json,但这并不是我想要的。我想使用Android提供的ONLY工具来完成阅读这个json。这是我的json
我正在使用 Android 4.0.3
json 文件数据:
{
"offers": [
{
"id": "1",
"type":"coupon",
"description":"half off at the GAP",
"url":"http://www.gap.com/"
},
{
"id": "1",
"type":"offer",
"description":"buy one get one at Ann Taylor",
"url":"http://www.anntaylor.com/"
},
{
"id": "1",
"type":"type:promotion",
"description":"free kids scoop for first ten people",
"url":"http://www.gap.com/"
}
]
}
MainActivity.java
package com.pega.parsejson;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView jsonWrapper;
HttpClient httpClient;
JSONObject json;
final static String offersUrl="http://somewebsite.com/offers.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jsonWrapper=(TextView) findViewById(R.id.jsonWrap);
httpClient=new DefaultHttpClient();
new Read().execute("description");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public JSONObject jsonOffers() throws ClientProtocolException, IOException, JSONException {
//StringBuilder url=new StringBuilder(offersUrl);
HttpGet get=new HttpGet(offersUrl);
HttpResponse res=httpClient.execute(get);
int status=res.getStatusLine().getStatusCode();
if(status==200){
HttpEntity e=res.getEntity();
String data=EntityUtils.toString(e);
JSONArray timeline=new JSONArray("offers");
JSONObject jsonOffer=timeline.getJSONObject(0); //returns most recent offer
return jsonOffer;
}else{
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
return null;
}
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
try {
json=jsonOffers();
return (String) json.get("description"); //returns string w/ parameter passed in as "description"
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
jsonWrapper.setText(result);
}
}
}
我也在使用INTERNET PERMISSIONS:
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pega.parsejson"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.pega.parsejson.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/jsonWrap"
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="" />
</RelativeLayout>
logcat :
[2013-08-07 23:35:41 - parseJson] ------------------------------
[2013-08-07 23:35:41 - parseJson] Android Launch!
[2013-08-07 23:35:41 - parseJson] adb is running normally.
[2013-08-07 23:35:41 - parseJson] Performing com.pega.parsejson.MainActivity activity launch
[2013-08-07 23:35:42 - parseJson] Uploading parseJson.apk onto device 'HT25EHX00690'
[2013-08-07 23:35:42 - parseJson] Installing parseJson.apk...
[2013-08-07 23:35:45 - parseJson] Success!
[2013-08-07 23:35:45 - parseJson] Starting activity com.pega.parsejson.MainActivity on device HT25EHX00690
[2013-08-07 23:35:46 - parseJson] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.pega.parsejson/.MainActivity }
请注意我不想使用任何json库来解析我的json,我严格地想使用ONLY来解析json。我只是不明白为什么我的mainActivity.java没有在手机上显示任何内容。
为我的帖子的长度道歉,对于时间敏感的项目真的需要帮助,是的,我已经在Stackoverflow上阅读了其他类似的问题,似乎没有人回答这个问题。
有人可以帮帮我吗?
由于
答案 0 :(得分:1)
尝试这样的事情:
JSONObject jObj;
JSONArray data = null;
JSONObject jsonOffer;
jObj = new JSONObject(data);
data = jObj.getJSONArray("offers");
jsonOffer=data.getJSONObject(0);
希望它能帮助!!