Android:使用带端口号的Uri.Builder()。build()创建一个URL

时间:2015-10-31 17:38:34

标签: java android apache

Uri.Builder.build()可以很好地处理普通网址,但是在端口号支持下失败了。

我发现使其支持端口号的最简单方法是首先解析给定的URL,然后使用它。

        private void postData(String value_id,String  value_seaction,
        String value_item,
        String value_descration) {

        HttpParams httpParameters = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
        HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);

           HttpClient httpclient = new DefaultHttpClient(httpParameters);
          // Log.d("url=>",value_item);
          // Log.d("url=>","http://rafsanjan.uni-azad.ebrahiminezhad.ir/json/darkhasr.php?shdaneshjo="+value_id+"&moavenat="+value_seaction+"&darkhast="+value_item+"&startdate=test&tozih="+ value_descration);
        //   http://app.sirmagid.ac.ir:1180/json2
           Uri.Builder builder = new Uri.Builder();
           builder.scheme("http")
               .authority("app.iaurafsanjan.ac.ir:1180")
               .appendPath("json2")
               .appendPath("darkhasr.php")
               .appendQueryParameter("shdaneshjo", value_id)
               .appendQueryParameter("moavenat", value_seaction)
                .appendQueryParameter("darkhast", value_item)
                 .appendQueryParameter("startdatet", "0")
                 .appendQueryParameter("tozih", value_descration);
              // .fragment("section-name");
           String myUrl = builder.build().toString();
           Log.d("url=>",myUrl);

          HttpPost httppost = new HttpPost(myUrl);
          // HttpPost httppost = new HttpPost
           ("http://sirmagid.uni-azad.ebrahiminezhad.ir   /json/darkhasr.php?shdaneshjo="+value_id+"&moavenat="+value_seaction+"&darkhast="+zir_item+"&startdate=test&tozih="+ value_descration);  //???
           try {
               ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(8);
               //nameValuePairs.add(new BasicNameValuePair("name", name));
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
               HttpResponse response = httpclient.execute(httppost);
               HttpEntity resEntity = response.getEntity();  
               Log.d("RESPONSE",EntityUtils.toString(resEntity));

           }
           catch(Exception e)
           {
               Log.e("log_tag", "Error:  "+e.toString());
           }
       }

返回:

http://app.sirmagid.ac.ir%3a1180/json2/darkhasr?shdaneshjo=920275234&moavenat=%D8%A7%D9%85%D9%88%D8%B2%D8%B4%DB%8C&darkhast=j&startdatet=0&tozih=66666

错误:

%3a1180/

2 个答案:

答案 0 :(得分:13)

没有特殊的方法来设置端口号。 要实现您的目标,您应该使用.encodedAuthority("app.iaurafsanjan.ac.ir:1180")代替.authority("app.iaurafsanjan.ac.ir:1180")

答案 1 :(得分:1)

我在android studio中移动我的项目并添加此代码 的build.gradle

  dependencies {
  compile 'com.android.support:support-v4:22.2.1'
  compile group: 'org.apache.httpcomponents' ,
  name: 'httpclient-  android' , version: '4.3.5.1'
  }

删除 import android.net.Uri; 加          import org.apache.http.client.HttpClient;          import org.apache.http.client.entity.UrlEncodedFormEntity;          import org.apache.http.client.methods.HttpGet;          import org.apache.http.client.methods.HttpPost;          import org.apache.http.client.utils.URIBuilder;

    private void postData(String value_id,String value_seaction,String value_item,String value_descration) {

        HttpParams httpParameters = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
        HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);

           HttpClient httpclient = new DefaultHttpClient(httpParameters);
          // HttpPost httppost = new HttpPost("http://rafsanjan.uni-azad.ebrahiminezhad.ir/json/darkhasr.php?shdaneshjo="+value_id+"&moavenat="+value_seaction+"&darkhast="+zir_item+"&startdate=test&tozih="+ value_descration);  //???
           try {
               URIBuilder builder = new URIBuilder();
               builder.setScheme("http")
                       .setHost("app.iaurafsanjan.ac.ir")
                       .setPort(1180)
                       .setPath("/json2/darkhasr.php")
                       .addParameter("shdaneshjo", value_id)
                       .addParameter("moavenat", value_seaction)
                       .addParameter("darkhast", value_item)
                       .addParameter("startdatet", "0")
                       .addParameter("tozih", value_descration)
                       .build();
               // .fragment("section-name");
               String myUrl = builder.toString();
               Log.d("url=>",myUrl);

               HttpPost httppost = new HttpPost(myUrl);

               ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(8);
               //nameValuePairs.add(new BasicNameValuePair("name", name));
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
               HttpResponse response = httpclient.execute(httppost);
               HttpEntity resEntity = response.getEntity();  
               Log.d("RESPONSE",EntityUtils.toString(resEntity));

           }
           catch(Exception e)
           {
               Log.e("log_tag", "Error:  "+e.toString());
           }


       }

apache.org

apache.org

http://grepcode.com