dougan1330 2013-05-17 11:59
浏览 48
已采纳

通过Android应用程序连接到PHP脚本[重复]

This question already has an answer here:

This is the code I have so far.

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()

Can somebody please tell me what is wrong in the following code! I have established that there is a problem in the try catch block only and not anywhere else in the activity. I just do not know what it is or how to correct it.

My PHP code is quite simple. It is something like this -

//code to reverse the string
$reversed = strrev($_POST["action"]);
echo $reversed;
</div>
  • 写回答

3条回答 默认 最新

  • douguanya6399 2013-05-17 12:32
    关注

    Adding as a seperate answer as the poster requests to do so .

    Assumption :

    a ) There is a text box that accepts the URL to load

    b ) A button which when clicked performs the networking operation on the URL fetched f

    Implement a button click listener that calls the following function :

        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 + "
    ");
                    }
                    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);
            }
    

    You need to implement onPostExecute as well . There are other APIS .

    Android Async Task Documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建