-1

这是我到目前为止的代码。

public void postData(String toPost) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.mywebsite.com/dev/reverser.php");

    //This is the data to send
    String MyName = toPost; //any data to send

    try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("action", MyName));

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String response = httpclient.execute(httppost, responseHandler);

    //This is the response from a php application

    String reverseString = response;
    Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

    } catch (ClientProtocolException e) {
    Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    } catch (IOException e) {
    Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    }

    }//end postData()

有人可以告诉我以下代码有什么问题!我已经确定仅在 try catch 块中存在问题,而在活动中的其他任何地方都没有。我只是不知道它是什么或如何纠正它。

我的 PHP 代码非常简单。是这样的——

//code to reverse the string
$reversed = strrev($_POST["action"]);
echo $reversed;
4

3 回答 3

1
于 2013-05-17T12:11:01.590 回答
0

作为发布者要求的单独答案添加。

假设 :

a) 有一个文本框接受要加载的 URL

b ) 一个按钮,点击后对获取的 URL 执行网络操作 f

实现一个按钮点击监听器,它调用以下函数:

    private void URL() 
    {
        String url = txtURL.getText().toString();
        new URLTask().execute(new String[] { url });
    }    




private class URLTask extends AsyncTask<String, Void, String> 
    {
        protected String doInBackground(String... urls)
        {

            BufferedReader br = null;
            String url = urls[0];
            StringBuffer sb = new StringBuffer("");

            try
            {
                HttpClient client = new DefaultHttpClient();
                HttpPost request = new HttpPost();
                request.setURI(new URI(url));

                List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("param1", "value of param1")); 

                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);         

                request.setEntity(formEntity);
                HttpResponse response = client.execute(request);

                String line;

                br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = br.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                br.close();

            } 
            catch(Exception e)
            {
                Toast.makeText(HttpPostActivity.this, "Exception: " + e.toString(), Toast.LENGTH_LONG).show();
            }
            finally 
            {
                if (br != null) 
                {
                    try 
                    {
                        br.close();
                    } 
                    catch(IOException ioe) 
                    {
                        ioe.printStackTrace();
                    }
                }           

            }           

            return(sb.toString());
        }


        protected void onPostExecute(String result) 
        {
            txtContent.setText(result);
        }

您还需要实现 onPostExecute 。还有其他 APIS 。

Android 异步任务文档

于 2013-05-17T12:32:40.597 回答
0

尝试打印出异常。使用此代码打印出您的回复。检查响应的状态

        HttpResponse response = client.execute(request);

        String line;

        br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        while ((line = br.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        br.close();

    } 
    catch(Exception e)
    {
        Toast.makeText(HttpPostActivity.this, "Exception: " + e.toString(), Toast.LENGTH_LONG).show();
    }

告诉我们例外情况。那时很容易查明您的问题。

编辑:答案:

您正试图在 MAIN 线程上执行网络操作。这是违法的事情。创建一个 AsyncTask 即创建一个单独的线程来执行您的网络操作。

Android 异常详情

堆栈溢出问题

于 2013-05-17T12:07:56.623 回答