在Uri构建器中编码“/”和“@”字符

时间:2017-09-16 12:56:22

标签: android uri

以下代码生成UnkownHost异常:

    public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener{
    private Button send;
    private EditText to;
    private EditText subject;
    private EditText message;

    //Configure here your HERE
    private final String URL = "www.chopapp.co/sendEmail4.php";

    //Parameters, just for demo
    private final String[] parameters = new String[]{
            "to",
            "subject",
            "message"
    };

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


        send = (Button) findViewById(R.id.send);
        to = (EditText) findViewById(R.id.destination);
        subject = (EditText) findViewById(R.id.subject);
        message = (EditText) findViewById(R.id.message);


        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        SingletonRequest request = SingletonRequest.getInstance(this);

        Uri.Builder builder = new Uri.Builder();


        builder.scheme("http")
                .authority(URL)
                .appendQueryParameter(parameters[0], to.getText().toString())
                .appendQueryParameter(parameters[1], subject.getText().toString())
                .appendQueryParameter(parameters[2],subject.getText().toString());

        String finalUrl = builder.toString();

        Log.d("theURL", finalUrl.toString());

        StringRequest stringRequest = new StringRequest(Request.Method.GET, finalUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(MainActivity.this, "Request Completed", Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "Problem with Request" + error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }
        });

        request.addToRequestQueue(stringRequest);
    }
}

生成的输出网址为:http://www.chopapp.co%2FsendEmail4.php?to=myemail%40mail.com&subject=sub&message=msg

我们如何处理“/”&amp; “@”字符现在在我的URL中编码为%2F和%40。

1 个答案:

答案 0 :(得分:1)

您需要使用URLDecoder

解码网址
String afterDecodeURL = URLDecoder.decode(finalUrl, "UTF-8");

然后将afterDecodeURL传递给凌空StringRequest