Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Create a URL using Uri.Builder().build() with port numbers

Uri.Builder.build() works quite well with normal URLs, but it fails with port number support.

The easiest way that I discovered to make it support port numbers was to make it parse a given URL first then work with it.

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

return:

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

error:

%3a1180/

like image 835
sirmagid Avatar asked Oct 31 '15 17:10

sirmagid


People also ask

What is URI builder?

UriBuilder(String, String, Int32, String, String) Initializes a new instance of the UriBuilder class with the specified scheme, host, port number, path, and query string or fragment identifier. UriBuilder(Uri) Initializes a new instance of the UriBuilder class with the specified Uri instance.

How do I create an HTTP URL in Java?

In your Java program, you can use a String containing this text to create a URL object: URL myURL = new URL("http://example.com/"); The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question.

What is the format of URI in Android?

In Android that means a URI, Uniform Resource Identifier[^]. The Android API contains the definition as, Uri [^] in android.net package. // Changed // 1. getdata to getData // 2.

What is the use of URI in Android?

URI(Uniform resource identifier) as its name suggests is used to identify resource(whether it be a page of text, a video or sound clip, a still or animated image, or a program). The most common form of URI is the Web page address, which is a particular form or subset of URI called a Uniform Resource Locator (URL).


1 Answers

There is no special method to set a port number. To achieve what you want, you should use .encodedAuthority("app.iaurafsanjan.ac.ir:1180") instead of .authority("app.iaurafsanjan.ac.ir:1180").

like image 177
Mikhail Avatar answered Oct 13 '22 04:10

Mikhail