我试图通过使用keyvalue对
来解析JSONAndroidTabRestaurantDescSearchListView.java
public class AndroidTabRestaurantDescSearchListView extends TabActivity {
// TabSpec Names
private static final String INBOX_SPEC = "Rating";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
String REST = getIntent().getStringExtra("REST");
// Inbox Tab
TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
Intent inboxIntent = new Intent(this, RatingDescriptionSearchActivity.class);
inboxIntent.putExtra("REST", REST);
inboxSpec.setIndicator(INBOX_SPEC);
// Tab Content
inboxSpec.setContent(inboxIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(inboxSpec);
//Set the current value tab to default first tab
tabHost.setCurrentTab(0);
}
}
RatingDescriptionSearchActivity.java
public class RatingDescriptionSearchActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String NAME = "rank";
static String TYPE = "country";
static String DISTANCE = "distance";
static String RATING = "rating";
static String FLAG = "flag";
static String PRICE= "price";
String url="http://54.218.73.244:7005/DescriptionSortedSearchRating/";
String CITY;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
CITY = getIntent().getStringExtra("REST");
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(RatingDescriptionSearchActivity.this);
// Set progressdialog title
//mProgressDialog.setTitle("Fetching the information");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
String newurl = url + "?" + "Key=" + CITY;
jsonobject = JSONfunctions.getJSONfromURL(newurl);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("restaurants");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put(RatingDescriptionActivity.NAME, jsonobject.getString("restaurantNAME"));
map.put(RatingDescriptionActivity.TYPE, jsonobject.getString("restaurantTYPE"));
map.put(RatingDescriptionActivity.FLAG, "http://54.218.73.244:7005/"+jsonobject.getString("restaurantIMAGE"));
map.put(RatingDescriptionActivity.DISTANCE, jsonobject.getString("restaurantDISTANCE"));
map.put(RatingDescriptionActivity.RATING, jsonobject.getString("restaurantRATING"));
map.put(RatingDescriptionActivity.PRICE, jsonobject.getString("restaurantPrice"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(RatingDescriptionSearchActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
Searchpage.java
public class SearchPage extends FragmentActivity {
EditText mEdit;
EditText City;
Button Back;
Button Search;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
City=(EditText) findViewById(R.id.CITY_ID);
Back=(Button) findViewById(R.id.TopNavigationBarRestaurantSearchActivityBackButton);
Back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Takes you to whichever activity launched C
finish();
}
});
Search=(Button) findViewById(R.id.SEARCH_BUTTON_ID);
Search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent searchIntent=new Intent(SearchPage.this,AndroidTabRestaurantDescSearchListView.class);
searchIntent.putExtra("REST",City.getText().toString());
startActivity(searchIntent);
}
});
}
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText)findViewById(R.id.DATE_EDIT_TEXT_ID);
mEdit.setText(day+"/"+month+"/"+year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, dd, mm+1);
}
}
}
{EDIT]
我更改了jsonobject = JSONfunctions.getJSONfromURL(newurl);
LOG ::
10-10 14:57:46.326: E/log_tag(1116): Error in http connection java.lang.IllegalArgumentException: Illegal character in query at index 65: http://54.218.73.244:7005/DescriptionSortedSearchRating/?Key=pune
10-10 14:57:46.326: E/log_tag(1116): Error converting result java.lang.NullPointerException
10-10 14:57:46.326: E/log_tag(1116): Error parsing data org.json.JSONException: End of input at character 0 of
10-10 14:57:46.326: W/dalvikvm(1116): threadid=14: thread exiting with uncaught exception (group=0x40015560)
10-10 14:57:46.436: E/AndroidRuntime(1116): FATAL EXCEPTION: AsyncTask #2
10-10 14:57:46.436: E/AndroidRuntime(1116): java.lang.RuntimeException: An error occured while executing doInBackground()
10-10 14:57:46.436: E/AndroidRuntime(1116): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.lang.Thread.run(Thread.java:1019)
10-10 14:57:46.436: E/AndroidRuntime(1116): Caused by: java.lang.NullPointerException
10-10 14:57:46.436: E/AndroidRuntime(1116): at com.project.findmybuffet.RatingDescriptionSearchActivity$DownloadJSON.doInBackground(RatingDescriptionSearchActivity.java:81)
10-10 14:57:46.436: E/AndroidRuntime(1116): at com.project.findmybuffet.RatingDescriptionSearchActivity$DownloadJSON.doInBackground(RatingDescriptionSearchActivity.java:1)
10-10 14:57:46.436: E/AndroidRuntime(1116): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-10 14:57:46.436: E/AndroidRuntime(1116): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-10 14:57:46.436: E/AndroidRuntime(1116): ... 4 more
10-10 14:57:50.696: E/WindowManager(1116): Activity com.project.findmybuffet.AndroidTabRestaurantDescSearchListView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405b70f8 that was originally added here
10-10 14:57:50.696: E/WindowManager(1116): android.view.WindowLeaked: Activity com.project.findmybuffet.AndroidTabRestaurantDescSearchListView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405b70f8 that was originally added here
10-10 14:57:50.696: E/WindowManager(1116): at android.view.ViewRoot.<init>(ViewRoot.java:258)
10-10 14:57:50.696: E/WindowManager(1116): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-10 14:57:50.696: E/WindowManager(1116): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-10 14:57:50.696: E/WindowManager(1116): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.Dialog.show(Dialog.java:241)
10-10 14:57:50.696: E/WindowManager(1116): at com.project.findmybuffet.RatingDescriptionSearchActivity$DownloadJSON.onPreExecute(RatingDescriptionSearchActivity.java:67)
10-10 14:57:50.696: E/WindowManager(1116): at android.os.AsyncTask.execute(AsyncTask.java:391)
10-10 14:57:50.696: E/WindowManager(1116): at com.project.findmybuffet.RatingDescriptionSearchActivity.onCreate(RatingDescriptionSearchActivity.java:50)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-10 14:57:50.696: E/WindowManager(1116): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-10 14:57:50.696: E/WindowManager(1116): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-10 14:57:50.696: E/WindowManager(1116): at android.widget.TabHost.addTab(TabHost.java:216)
10-10 14:57:50.696: E/WindowManager(1116): at com.project.findmybuffet.AndroidTabRestaurantDescSearchListView.onCreate(AndroidTabRestaurantDescSearchListView.java:59)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-10 14:57:50.696: E/WindowManager(1116): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 14:57:50.696: E/WindowManager(1116): at android.os.Looper.loop(Looper.java:123)
10-10 14:57:50.696: E/WindowManager(1116): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-10 14:57:50.696: E/WindowManager(1116): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 14:57:50.696: E/WindowManager(1116): at java.lang.reflect.Method.invoke(Method.java:507)
10-10 14:57:50.696: E/WindowManager(1116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-10 14:57:50.696: E/WindowManager(1116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-10 14:57:50.696: E/WindowManager(1116): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
使用
jsonobject = JSONfunctions.getJSONfromURL(newurl);
而不是
jsonobject = JSONfunctions.getJSONfromURL("newurl");
因为目前您正在将newurl
而不是newurl String的值传递给getJSONfromURL
与更新日志一样:
http连接错误java.lang.IllegalArgumentException:非法 索引65处的查询中的字符: ..... DescriptionSortedSearchRating /?答案=普纳
所以从网址末尾删除/
答案 1 :(得分:0)
你的第一个错误是
http连接错误java.lang.IllegalArgumentException:索引65处的查询中出现非法字符:
http://54.218.73.244:7005/DescriptionSortedSearchRating/?Key=pune
这是由于查询末尾的额外空间。尝试在添加之前修剪字符串(或者更好的是,在CITY参数上使用URLEncoder)。
答案 2 :(得分:0)
试试这个。
JSONObject jObject = new JSONObject(str_response_starter);
JSONArray json_array_item_id = jObject.getJSONArray("itemid");
System.out.println("json array item id"+json_array_item_id);
如果您提供json格式会更好。可以使用集合
来解析JSON