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/
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")
.
i move my project in android studio and add this code to 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'
}
remove import android.net.Uri; add 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());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.