1

私は Android Web サービスを初めて使用します。PHP サーバーと通信したいのですが、応答コードは 200 ですが、Buffered Reader は readline で null を返します。

何が問題なのかわからない。親切に私を助けて

以下を使用して AsyncTask タスクを実行しています。

new AsysnchronousTask().execute("");

AsyncTask の完全なコードは次のとおりです。

public class AsysnchronousTask extends AsyncTask<String, String, String> {

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            Log.d(" sever Resutl ", result);

        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            String result = "";
            HttpClient mDefaultHttpClient = new DefaultHttpClient();
            HttpPost mHttpPost = new HttpPost(PATH);
            // JSONObject sendJsonObject= new JSONObject();
            JSONObject postJsonObject = new JSONObject();

            HttpResponse mResponse;

            try {

                try {
                    postJsonObject.put("email", "hotspot.st@gmail.com");
                    postJsonObject.put("login", "alan");
                    postJsonObject.put("password", "120519");
                    postJsonObject.put("language", "en");

                    JSONArray mJsonArray = new JSONArray();
                    mJsonArray.put(postJsonObject);

                    mHttpPost.setHeader("REGISTER", postJsonObject.toString());
                    mHttpPost.getParams().setParameter("jsonpost", mJsonArray);

                    // post the data

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mResponse = mDefaultHttpClient.execute(mHttpPost);

                StatusLine statuscod = mResponse.getStatusLine();

                int statuscode = statuscod.getStatusCode();

                Header[]  mHeader= mResponse.getAllHeaders();



                if (statuscode == 200) {
                    HttpEntity mEntity = mResponse.getEntity();

                    if (mEntity != null) {

                        InputStream mInputStream = mEntity.getContent();

                        // Converting Stream into the string

                        BufferedReader mBufferedReader = new BufferedReader(
                                new InputStreamReader(mInputStream));

                        StringBuilder mBuilder = new StringBuilder();

                        String line = null;

                        while ((line = mBufferedReader.readLine()) != null) {

                            mBuilder.append(line + "/n");

                        }


                        mBufferedReader.close();
                        mInputStream.close();


                    }

                    else {

                        Log.d(TAG, "Fail to Read from Server ");

                    }
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                JSONArray mJsonArray = new JSONArray(result);
                JSONObject mJsonObject = null;

                for (int i = 0; i < mJsonArray.length(); i++) {

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return result;

        }

    }
4

1 に答える 1

0

応答の問題を解決しようとする前でも、コードを改善できる方法がいくつかあります。情報を送信する際に、JSON の代わりに BasicNameValuePair ArrayList を実装することをお勧めします。あなたの場合、その実装は次のようになります。

enter code  InputStream is = null;
String result = "";
String key = "";
ArrayList<SharePrices> results = new ArrayList<SharePrices>();

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "hotspot.st@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("login", "alan"));
nameValuePairs.add(new BasicNameValuePair("password", "120519"));
nameValuePairs.add(new BasicNameValuePair("language", "en"));

// http post
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(PATH);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();

} catch (Exception e) {
    Log.e("log_tag", "Error in connection " + e.toString());
    // results.setText("Error in connection");
}

// convert response to string
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");
    }
    is.close();
    result = sb.toString();
} catch (Exception e) {
    Log.e("log_tag", "Error converting result " + e.toString());

} here
于 2013-10-09T06:35:41.787 に答える