java—将数据从设备发送到远程服务器

cwtwac6a  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(300)

我的应用程序正在将两个值从设备传递到远程服务器,但当我在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");
                }       
            }
        });  
    }
}
62o28rlo

62o28rlo1#

它可能由于networkonmainthreadexception而崩溃。我试图在一个内部类中执行一个http请求 extends 班级 AsyncTask .
我把所有与网络有关的东西都放在 doInBackground() 方法。

0sgqnhkj

0sgqnhkj2#

如果出现错误:networkonmainthreadexception
在内部类中执行http请求 extends 班级 AsyncTask .
或者

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

试试这个,或者你可以使用截击,改装或其他网络库的api调用。

相关问题