从设备向远程服务器发送数据

时间:2015-04-28 07:03:01

标签: java android

我的应用程序正在将两个值从设备传递到远程服务器,但是当我在bluestack中进行测试时,当我在真实设备中安装时,它会显示一条消息,例如"不幸的是应用程序已被弯曲"所以我不能站在哪里有问题我删除和清理设备临时和设备的缓存内存仍然无法输出。

以下是我的源代码

package com.androidexample.httpgetexample;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;  
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class HttpGetAndroidExample extends Activity {

    TextView content;
    EditText fname,email,login,pass;
    Spinner sp;
    Button b1;
    String s[] = { "Courtage Problem", "Cartage Refil", "Printer Problem",
    "Printer Drivers" };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_get_android_example);

        content = (TextView)findViewById(R.id.content);
        fname   = (EditText)findViewById(R.id.name);
        sp  = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> ad = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, s);
        sp.setAdapter(ad);
        ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Button saveme=(Button)findViewById(R.id.save);


        saveme.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v)
            {   
                //ALERT MESSAGE
                Toast.makeText(getBaseContext(),
                        "Please wait, connecting to server.",
                        Toast.LENGTH_LONG).show();
            try{    

                String n = URLEncoder.encode(fname.getText().toString(), "UTF-8");
                String d = URLEncoder.encode(sp.getSelectedItem().toString(), "UTF-8");

                    HttpClient Client = new DefaultHttpClient();
                    String URL = "http://shreebijapur.in/Customerquery.aspx?n="+n+"&d="+d;

                    //Log.i("httpget", URL);
                    try
                    {
                        HttpGet httpget = new HttpGet(URL);
                        ResponseHandler<String> responseHandler = new BasicResponseHandler();

                        String SetServerString = "";
                        SetServerString = Client.execute(httpget, responseHandler);
                        content.setText(SetServerString);
                    }
                    catch(Exception ex)
                    {
                        content.setText("Fail!");
                    }
                }
                catch(UnsupportedEncodingException ex)
                {
                    content.setText("Fail111");
                }       
            }
        });  
    }
}

2 个答案:

答案 0 :(得分:0)

由于networkOnMainThreadException,它可能会崩溃。我试图在extendsAsyncTask的内部类中执行http请求。

我将所有与网络相关的内容放在doInBackground()方法中。

答案 1 :(得分:0)

如果错误:networkOnMainThreadException

extendsAsyncTask

的内部类中执行http请求

new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
               // HERE_YOUR_HTTP_REQUEST CODE
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);

            }
        }.execute();  

试试这个 要么 您可以使用volley,retrofit或其他网络库进行API调用。