0

JSON解析器类

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            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");
            }
              /*int i = 0,c; 
            while ((c = reader.read()) != -1) {
                sb.append((char)c);
                Log.d("test",String.valueOf(i++) + " " + (char)c);
            }*/
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

http://www.2shared.com/document/gH4zJsOO/log.html

将数据发送到 MySQL 数据库

06-09 21:46:02.650: D/dalvikvm(315): GC_FOR_MALLOC freed 6777 objects / 323848 bytes in 87ms
06-09 21:49:11.980: W/System.err(315): java.net.SocketException: The operation timed out
06-09 21:49:11.990: W/System.err(315):  at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
06-09 21:49:12.000: W/System.err(315):  at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
06-09 21:49:12.000: W/System.err(315):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
06-09 21:49:12.010: W/System.err(315):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
06-09 21:49:12.010: W/System.err(315):  at java.net.Socket.connect(Socket.java:1055)
06-09 21:49:12.020: W/System.err(315):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-09 21:49:12.030: W/System.err(315):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
06-09 21:49:12.030: W/System.err(315):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-09 21:49:12.040: W/System.err(315):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-09 21:49:12.050: W/System.err(315):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
06-09 21:49:12.050: W/System.err(315):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-09 21:49:12.060: W/System.err(315):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-09 21:49:12.060: W/System.err(315):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-09 21:49:12.070: W/System.err(315):  at tn.pack.ordre.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
06-09 21:49:12.080: W/System.err(315):  at tn.pack.ordre.library.UserFunctions.registerUser(UserFunctions.java:60)
06-09 21:49:12.090: W/System.err(315):  at tn.pack.ordre.EnregistrerActivity.onClickValider(EnregistrerActivity.java:49)
06-09 21:49:12.090: W/System.err(315):  at java.lang.reflect.Method.invokeNative(Native Method)
06-09 21:49:12.100: W/System.err(315):  at java.lang.reflect.Method.invoke(Method.java:521)
06-09 21:49:12.110: W/System.err(315):  at android.view.View$1.onClick(View.java:2067)
06-09 21:49:12.120: W/System.err(315):  at android.view.View.performClick(View.java:2408)
06-09 21:49:12.120: W/System.err(315):  at android.view.View$PerformClick.run(View.java:8816)
06-09 21:49:12.130: W/System.err(315):  at android.os.Handler.handleCallback(Handler.java:587)
06-09 21:49:12.140: W/System.err(315):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 21:49:12.150: W/System.err(315):  at android.os.Looper.loop(Looper.java:123)
06-09 21:49:12.150: W/System.err(315):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-09 21:49:12.160: W/System.err(315):  at java.lang.reflect.Method.invokeNative(Native Method)
06-09 21:49:12.160: W/System.err(315):  at java.lang.reflect.Method.invoke(Method.java:521)
06-09 21:49:12.170: W/System.err(315):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-09 21:49:12.180: W/System.err(315):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-09 21:49:12.180: W/System.err(315):  at dalvik.system.NativeStart.main(Native Method)
06-09 22:13:27.330: E/Buffer Error(315): Error converting result java.lang.NullPointerException
06-09 22:13:27.360: E/JSON Parser(315): Error parsing data org.json.JSONException: End of input at character 0 of 
4

1 回答 1

0

问题出在 EasyPHP-5.3.9 我用 EasyPHP-2.0b1 更改了版本,它可以工作

于 2012-06-14T17:56:05.050 回答