我正在尝试通过向服务器发出发布请求来动态地将数据传递给下一个活动
我的分析::
- 我无法确定错误是在 express 上的服务器上还是在 android 客户端代码上
或者
- 我是否在异步任务中向服务器发出正确的发布请求
[笔记]
来自Url
变量的数据显示在列表视图中但是列表行的 Onclick .... 需要向服务器发出发布请求并且必须检索 JSON 响应的地方是出现错误的地方
MainActivity.java
public class MainActivity extends Activity {
// url to make request
private static String url = "http://54.218.73.244:7003/";
private static String url1 = "http://54.218.73.244:7003/RestaurantDesc/";
String item;
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 with onCreate method
progressDialog=new ProgressDialog(MainActivity.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String _response = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
_response =EntityUtils.toString(resEntity);
JSONObject jsonObject = new JSONObject(_response);
JSONArray first_array = jsonObject.getJSONArray("restaurants");
JSONArray second_array = jsonObject.getJSONArray("RestaurantTimings");
for(int i=0;i<first_array.length();i++)
{
JSONObject c = second_array.getJSONObject(i);
Item item = new Item();
// Storing each json item in variable
int id = c.getInt("_id");
String TIME = c.getString("RestaurantTime");
item.setTime(TIME);
c = first_array.getJSONObject(i);
String NAME=c.getString("restaurantNAME");
item.setName(NAME);
String URL=c.getString("url");
item.setUrl(URL);
yourData.add(item);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException 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 timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);
ListView yourListView = (ListView) findViewById(R.id.listViewID);
ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
yourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
item = yourData.get(position).getName();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url1);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Key", item));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Intent i = new Intent(MainActivity.this, RestaurantDesc.class);
i.putExtra("key", url1);
i.putExtra("CC_RES",item.toString());
startActivity(i);
}
});
}
}
}
服务器上的部分 Express 代码::
app.get('/RestaurantDesc/:Key',function(request,response,next){
var keyName=request.params.Key;
var name_of_restaurants, RestaurantTimings;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM ', keyName, function(err, rows, fields)
{
console.log('Connection result error '+err);
name_of_restaurants = rows;
callback();
});
},
// Get the second table contents
function ( callback ) {
connection.query('SELECT * FROM ', RestaurantTimings, function(err, rows, fields)
{
console.log('Connection result error '+err);
RestaurantTimings = rows;
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'restaurants' : name_of_restaurants,
'RestaurantTimings' : RestaurantTimings
});
} );
} );
[日志]
08-29 08:20:01.963: W/dalvikvm(333): threadid=6: spin on suspend #1 threadid=9 (pcf=0)
08-29 08:20:01.963: D/dalvikvm(333): Temporarily moving tid 342 to fg (was 0)
08-29 08:20:01.963: D/dalvikvm(333): Temporarily raised priority on tid 342 (10 -> 0)
08-29 08:20:02.041: W/dalvikvm(333): threadid=6: spin on suspend resolved in 1081 msec
08-29 08:20:02.041: D/dalvikvm(333): Restored policy of 342 to 0
08-29 08:20:02.041: D/dalvikvm(333): Restored priority on 342 to 10
08-29 08:20:12.541: D/dalvikvm(333): GC_EXTERNAL_ALLOC freed 177K, 48% free 3078K/5895K, external 2111K/2137K, paused 86ms
08-29 08:20:18.042: W/System.err(333): org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONObject
08-29 08:20:18.042: W/System.err(333): at org.json.JSON.typeMismatch(JSON.java:107)
08-29 08:20:18.062: W/System.err(333): at org.json.JSONObject.<init>(JSONObject.java:158)
08-29 08:20:18.062: W/System.err(333): at org.json.JSONObject.<init>(JSONObject.java:171)
08-29 08:20:18.062: W/System.err(333): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:114)
08-29 08:20:18.062: W/System.err(333): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-29 08:20:18.072: W/System.err(333): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-29 08:20:18.072: W/System.err(333): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-29 08:20:18.072: W/System.err(333): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-29 08:20:18.072: W/System.err(333): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-29 08:20:18.072: W/System.err(333): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-29 08:20:18.082: W/System.err(333): at java.lang.Thread.run(Thread.java:1019)
08-29 08:20:18.082: D/AndroidRuntime(333): Shutting down VM
08-29 08:20:18.082: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 08:20:18.112: E/AndroidRuntime(333): FATAL EXCEPTION: main
08-29 08:20:18.112: E/AndroidRuntime(333): java.lang.NullPointerException
08-29 08:20:18.112: E/AndroidRuntime(333): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-29 08:20:18.112: E/AndroidRuntime(333): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-29 08:20:18.112: E/AndroidRuntime(333): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-29 08:20:18.112: E/AndroidRuntime(333): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-29 08:20:18.112: E/AndroidRuntime(333): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-29 08:20:18.112: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 08:20:18.112: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
08-29 08:20:18.112: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 08:20:18.112: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 08:20:18.112: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
08-29 08:20:18.112: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 08:20:18.112: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 08:20:18.112: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
08-29 08:25:02.411: I/Process(333): Sending signal. PID: 333 SIG: 9
08-29 08:25:49.932: D/dalvikvm(364): GC_EXTERNAL_ALLOC freed 178K, 48% free 3078K/5895K, external 2111K/2137K, paused 88ms
08-29 08:25:51.571: W/System.err(364): org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONObject
08-29 08:25:51.701: W/System.err(364): at org.json.JSON.typeMismatch(JSON.java:107)
08-29 08:25:51.862: W/System.err(364): at org.json.JSONObject.<init>(JSONObject.java:158)
08-29 08:25:51.941: W/System.err(364): at org.json.JSONObject.<init>(JSONObject.java:171)
08-29 08:25:52.043: W/System.err(364): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:114)
08-29 08:25:52.081: W/System.err(364): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-29 08:25:52.081: W/System.err(364): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-29 08:25:52.171: W/System.err(364): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-29 08:25:52.231: W/System.err(364): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-29 08:25:52.251: W/System.err(364): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-29 08:25:52.261: W/System.err(364): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-29 08:25:52.301: W/System.err(364): at java.lang.Thread.run(Thread.java:1019)
08-29 08:25:52.331: D/AndroidRuntime(364): Shutting down VM
08-29 08:25:52.331: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 08:25:52.361: E/AndroidRuntime(364): FATAL EXCEPTION: main
08-29 08:25:52.361: E/AndroidRuntime(364): java.lang.NullPointerException
08-29 08:25:52.361: E/AndroidRuntime(364): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-29 08:25:52.361: E/AndroidRuntime(364): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-29 08:25:52.361: E/AndroidRuntime(364): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-29 08:25:52.361: E/AndroidRuntime(364): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-29 08:25:52.361: E/AndroidRuntime(364): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-29 08:25:52.361: E/AndroidRuntime(364): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 08:25:52.361: E/AndroidRuntime(364): at android.os.Looper.loop(Looper.java:123)
08-29 08:25:52.361: E/AndroidRuntime(364): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 08:25:52.361: E/AndroidRuntime(364): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 08:25:52.361: E/AndroidRuntime(364): at java.lang.reflect.Method.invoke(Method.java:507)
08-29 08:25:52.361: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 08:25:52.361: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 08:25:52.361: E/AndroidRuntime(364): at dalvik.system.NativeStart.main(Native Method)
08-29 08:26:12.072: I/Process(364): Sending signal. PID: 364 SIG: 9
08-29 08:26:38.353: D/dalvikvm(396): GC_EXTERNAL_ALLOC freed 179K, 48% free 3078K/5895K, external 2111K/2137K, paused 88ms
08-29 08:26:40.043: W/System.err(396): org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONObject
08-29 08:26:40.353: W/System.err(396): at org.json.JSON.typeMismatch(JSON.java:107)
08-29 08:26:40.562: W/System.err(396): at org.json.JSONObject.<init>(JSONObject.java:158)
08-29 08:26:40.562: W/System.err(396): at org.json.JSONObject.<init>(JSONObject.java:171)
08-29 08:26:40.562: W/System.err(396): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:114)
08-29 08:26:40.562: W/System.err(396): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-29 08:26:40.562: W/System.err(396): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-29 08:26:40.562: W/System.err(396): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-29 08:26:40.562: W/System.err(396): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-29 08:26:40.562: W/System.err(396): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-29 08:26:40.571: W/System.err(396): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-29 08:26:40.571: W/System.err(396): at java.lang.Thread.run(Thread.java:1019)
08-29 08:26:40.633: D/AndroidRuntime(396): Shutting down VM
08-29 08:26:40.633: W/dalvikvm(396): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 08:26:40.682: E/AndroidRuntime(396): FATAL EXCEPTION: main
08-29 08:26:40.682: E/AndroidRuntime(396): java.lang.NullPointerException
08-29 08:26:40.682: E/AndroidRuntime(396): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-29 08:26:40.682: E/AndroidRuntime(396): at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-29 08:26:40.682: E/AndroidRuntime(396): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-29 08:26:40.682: E/AndroidRuntime(396): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-29 08:26:40.682: E/AndroidRuntime(396): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-29 08:26:40.682: E/AndroidRuntime(396): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 08:26:40.682: E/AndroidRuntime(396): at android.os.Looper.loop(Looper.java:123)
08-29 08:26:40.682: E/AndroidRuntime(396): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 08:26:40.682: E/AndroidRuntime(396): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 08:26:40.682: E/AndroidRuntime(396): at java.lang.reflect.Method.invoke(Method.java:507)
08-29 08:26:40.682: E/AndroidRuntime(396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 08:26:40.682: E/AndroidRuntime(396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 08:26:40.682: E/AndroidRuntime(396): at dalvik.system.NativeStart.main(Native Method)
希望我清楚
谢谢