JSON から Android アクティビティに非同期的にデータを入力しようとしています
my JSON URL
::http://54.218.73.244:7002/RestaurantTimings
JSON STRUCTURE
::
[
{
"_id": 1,
"RestaurantTime": "8pm to 11pm"
},
{
"_id": 2,
"RestaurantTime": "10pm to 12pm"
},
{
"_id": 3,
"RestaurantTime": "11pm to 9pm"
},
{
"_id": 4,
"RestaurantTime": "10pm to 5pm"
}
]
これが私が試したコードです::
- アクティビティには、入力する必要がある 3 つのテキストビューがあります
- データを解析してコレクションに保存しました
- しかし、テキストビューにデータを入力する方法がわかりません
activity_main.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="match_parent"
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/restaurantTimings1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />
<TextView
android:id="@+id/restaurantTimings2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="133dp"
android:text="TextView" />
<TextView
android:id="@+id/restaurantTimings3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/restaurantTimings3"
android:layout_below="@+id/restaurantTimings3"
android:text="TextView" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
private static String url1 = "http://54.218.73.244:7002/RestaurantTimings";
private HashMap<Integer, String> TimeMap=new HashMap<Integer, String>();
List<Item> yourData = new ArrayList<Item>();
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//instantiating progressDialog
progressDialog=new ProgressDialog(MainActivity.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait .. ", true, false);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
JSONObjParser jParser=new JSONObjParser();
JSONArray json1=jParser.getJSONFromUrl(url1);
try{
for(int i=0;i<json1.length();i++){
JSONObject c=json1.getJSONObject(i);
Item item=new Item();
int id=c.getInt("_id");
String TIME=c.getString("RestaurantTime");
yourData.add(item);
}
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
TextView TV1=(TextView) findViewById(R.id.restaurantTimings1);
}
}
}
JSONObjParser.java
public class JSONObjParser {
static InputStream is = null;
static JSONArray jObj = null;
static String json = "";
// constructor
public JSONObjParser() {
}
public JSONArray getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
アイテム.java
public class Item {
private String Time;
public String getTime() {
return Time;
}
public void setTime(String Time) {
this.Time=Time;
}
}
- コードのほとんどの部分を完了しました
- しかし、私は
onPostExecute
方法を完了するのに苦労しています
任意のアイデア、