1
我創建一個應用程序,從Web服務獲取Json響應。該應用程序在2個文件中是一個webclient和一個webserviceActivity。客戶端回來了,我認爲logcat正在打印HTTP/1.1 200 OK。之後,似乎有一個JSONException。它可能與URL中傳遞的第二個參數的格式有關,因爲它是日期。我嘗試傳遞一個字符串,但無濟於事。我在這裏完成的方式是從sql.Date(yyyy mm dd)創建一個日期對象,並將其傳入。以這種方式,我在JSONException之前從服務器返回了一點。JSON響應不回來從.asmx web服務在android中
以下是這些文件。提前致謝。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ContentValues;
import android.util.Log;
public class WebClient {
private static final String TAG = WebClient.class.getSimpleName();
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/*
* This is a test function which will connects to a given rest service and
* prints it's response to Android Log with labels "Praeda".
*/
public JSONObject connect(String url) {
JSONObject json = null;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i(TAG, response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
Log.i(TAG, result);
// A Simple JSONObject Creation
json = new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
}
} 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 json;
}
}
。
import java.sql.Date;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class WebserviceActivity extends Activity {
private static final String TAG = WebserviceActivity.class.getSimpleName();
TextView tv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textviewwebserviceresponse);
WebClient rc = new WebClient();
@SuppressWarnings("deprecation")
Date d = new Date(2012, 9, 3);
JSONObject jObj = rc
.connect("http://212.169.27.217:84/rotaservice.asmx/GetRota?CarerID=36&RotaDate="+d);
Log.e(TAG, "json obj = " + jObj.toString());
tv.setText(jObj.toString());
}// end of onCreate
}// end of class
。
09-03 19:52:00.490: I/global(21931): In close() at SocketHttpClientConnection
09-03 19:52:00.490: I/global(21931): call createSocket() return a new socket.
09-03 19:52:03.610: I/WebClient(21931): HTTP/1.1 200 OK
09-03 19:52:03.610: I/WebClient(21931): <?xml version="1.0" encoding="utf-8"?>
09-03 19:52:03.610: I/WebClient(21931): <string xmlns="http://tempuri.org/">{"0":{"StartDate":"","EndDate":"","Duration":"0","CallStatusID":"0","CallStatusName":"","ClientSurname":"","ClientForename":"","NeedName":"Out of range","CarerAwayReason":"","CallID":"","DoubleUpCallID":""}}</string>
09-03 19:52:03.620: W/System.err(21931): org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
09-03 19:52:03.620: W/System.err(21931): at org.json.JSON.typeMismatch(JSON.java:111)
09-03 19:52:03.620: W/System.err(21931): at org.json.JSONObject.<init>(JSONObject.java:158)
09-03 19:52:03.620: W/System.err(21931): at org.json.JSONObject.<init>(JSONObject.java:171)
09-03 19:52:03.620: W/System.err(21931): at com.carefreegroup.WebClient.connect(WebClient.java:87)
09-03 19:52:03.620: W/System.err(21931): at com.carefreegroup.WebserviceActivity.onCreate(WebserviceActivity.java:31)
09-03 19:52:03.620: W/System.err(21931): at android.app.Activity.performCreate(Activity.java:4543)
09-03 19:52:03.620: W/System.err(21931): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-03 19:52:03.620: W/System.err(21931): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
09-03 19:52:03.620: W/System.err(21931): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
09-03 19:52:03.620: W/System.err(21931): at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-03 19:52:03.620: W/System.err(21931): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
09-03 19:52:03.620: W/System.err(21931): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 19:52:03.620: W/System.err(21931): at android.os.Looper.loop(Looper.java:156)
09-03 19:52:03.620: W/System.err(21931): at android.app.ActivityThread.main(ActivityThread.java:5045)
09-03 19:52:03.620: W/System.err(21931): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 19:52:03.630: W/System.err(21931): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 19:52:03.630: W/System.err(21931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-03 19:52:03.630: W/System.err(21931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-03 19:52:03.630: W/System.err(21931): at dalvik.system.NativeStart.main(Native Method)
09-03 19:52:03.630: D/AndroidRuntime(21931): Shutting down VM
好的,謝謝我會去找一個解析器 – turtleboy
其實只是一個更多的問題。將該Date對象追加到url的末尾,可以嗎? – turtleboy
@turtleboy Date對象將被轉換爲一個字符串,所以沒有問題。 – Musa