Android - 通过提交的文本传递网址

时间:2017-01-05 01:59:52

标签: java android http url request

我试图通过字段文本传递网址来制作HttpPost但我收到此错误:

Target host must not be null, or set in parameters. scheme=null, host=null, path=android.support.v7.widget.AppCompatEditText%7B1647e90+VFED..CL.+......I.+0%2C0-0%2C0+%237f0d00ad+app%3Aid%2Ffield_url%7D

这是我的代码

- 服务

class ThreadInBackgroud extends Thread{
    public void run(){
        try{

            Log.i("LOG", "LocationService.doInBackground()");
            HttpConnection.getSetDataWeb(URLEncoder.encode(Settings_Fragment.url,"UTF-8"),
                    "send-map-coords",
                    mMessageEB);
            stopSelf();

        }catch (Exception e) {
            Log.e("LOG", e.getMessage(),e);
        }

    }
}

-Fragment

Button saveButton = (Button) view.findViewById(R.id.btn_save);
    saveButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            SharedPreferences.Editor editorUrl = getActivity().getSharedPreferences("url_key", MODE_PRIVATE).edit();
            editorUrl.putString("url_key", url_field.getText().toString());
            editorUrl.commit();

        }
    });

    url = url_field.toString();

-HttpConnection

 public static String getSetDataWeb(String url, String method, MessageEB m) throws UnsupportedEncodingException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(URLEncoder.encode(url,"UTF-8"));

我想将网址作为变量传递,以便用户可以通过文本字段更改网址

3 个答案:

答案 0 :(得分:0)

更改

url = url_field.toString();

url = url_field.getText().toString();

因为需要调用getText()方法从输入字段获取文本,否则在View对象上调用toString()方法将提供string representation of it

答案 1 :(得分:0)

更改

url = url_field.toString();

url = url_field.getText().toString();

并将网址设为公开,以便您可以访问其他服务

public String url;

答案 2 :(得分:0)

您不应该对整个网址进行编码,因为这会导致像

这样的垃圾
EmployeeData

这将失败,因为它无法解析为普通的HTTP URL。

而是尝试使用

HttpPost httpPost = new HttpPost(" http://YOUR URL /...");