使用android app向服务器发送字符串消息

时间:2013-05-02 07:31:51

标签: android web send

我尝试了这段代码,但我收到了以下错误

当我点击按钮时,此消息显示“不幸的是app已停止”,然后退出我的应用程序。

public class testInput extends Activity {   

    Button Setbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test_input_page);

        Setbutton=(Button)findViewById(R.id.setbtn);


Setbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                new Asynactivity1().execute("");
                });     
    }

//这是我的Asyn课程

public class Asynactivity1 extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
    postData("data");
        return null;
}

//这是我的数据发布方法

  public void postData(String toPost) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://adastest.mysite.com/index.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
            }

            }


请帮帮我

谢谢

2 个答案:

答案 0 :(得分:0)

试试这个 在postdata方法中键入此代码并检查logcat中的响应。

HttpPost httppost = null;
    try {
        httppost = new HttpPost("http://adastest.mysite.com/index.php");
        HttpParams myParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(myParams, 10000);
        HttpConnectionParams.setSoTimeout(myParams, 30 * 10000);
        JSONObject jsonToken = new JSONObject();
        jsonToken.put("action", MyName);
        httppost.setHeader("Content-type", "application/json");
        HttpClient httpclient = new DefaultHttpClient(myParams);
        StringEntity se;
        if (jsonToken.toString() == null) {
            se = new StringEntity("", HTTP.UTF_8);
        } else {
            se = new StringEntity(jsonToken.toString(), HTTP.UTF_8);
        }
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        httppost.setEntity(se);

        // Execute HTTP Post Request
        HttpResponse response= httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();
        String getResult = EntityUtils.toString(entity);
        Log.e("response =", " " + getResult);


    } catch (Exception e) {
        e.printStackTrace();
    } 

答案 1 :(得分:0)

try {
                        System.out.println("inside server try 1");
                        // start - line is for sever connection/communication
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(
                        "http://10.0.0.8/insertitemdetails.php");
                        httppost.setEntity(new UrlEncodedFormEntity(
                                nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        // end - line is for sever connection/communication
                        InputStream is = entity.getContent();

                        Toast.makeText(getApplicationContext(),
                                "Send to server and inserted into mysql Successfully", Toast.LENGTH_LONG)
                                .show();
                    } catch (Exception e) {
                        Log.e("log_tag", "Error in http connection "
                                + e.toString());
                    }