JSON响应不是从android中的.asmx webservice回来的

时间:2012-09-03 18:54:28

标签: android web-services asmx

我正在创建一个从Web服务中获取Json响应的应用程序。该应用程序有2个文件,一个webclient和一个webserviceActivity。客户端回来确定我认为logcat正在打印HTTP / 1.1 200 OK。之后似乎有一个JSONException。它可能与url中传递的第二个参数的格式有关,因为它是一个日期。我试过传递一个字符串但无济于事。我在这里做的方法是从sql.Date(yyyy mm dd)创建一个日期对象并传入。这样我在JSONException之前从服务器返回一点

以下是文件。提前谢谢。

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

1 个答案:

答案 0 :(得分:0)

该服务在字符串tag中返回带有JSON的XML,您必须解析XML以获取它包含的JSON。

相关问题