URL error in Android - android

String temp1=(String)firstname.getText().toString();
String temp2=(String)lastname.getText().toString();
String urlreg="http://localhost/welcome.php?firstname="+temp1+"&lastname="+temp2;
Here firstname and lastname are the editText fields.
I am getting error as below.
LogCat error:
09-11 22:11:09.529: E/AndroidRuntime(1204): FATAL EXCEPTION: main
09-11 22:11:09.529: E/AndroidRuntime(1204): java.lang.IllegalArgumentException: Illegal character in query at index 54: http://localhost/welcome.php?firstname=ji&lastname=kij
The code works fine if I modify it to
String urlreg="http://localhost/welcome.php?firstname="+temp1+"&lastname";
Where am i going wrong?

Before using urlreg you need to url encode it to take out special characters using URLEncoder.encode().

Or you can do Httppost. It'll automatically encode your url.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("var_name", value));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.in/submit.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

Related

HttpPost encoding to windows-1255 add 25 to each char

I tried to send data to server with httpPost.
its look like this:
String username = URLEncoder.encode("myUsername","windows-1255");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("****");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username", username));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
But it's sending to the server char as %25XX and not %XX.
Why it's happening?
I was need to add the entity as string and not as list:
String param="username"+username;
post.setEntity(new StringEntity(param));

How to send Japanese characters using HttpPost

I'm trying to send Japanese characters to my API server but the characters sent were garbled and became ????. So I set the encoding to the entity using:
StringEntity stringEntity = new StringEntity(message, "UTF-8");
but the output became org.apache.http.entity.StringEntity#4316f850. I wonder if converting the stringEntity to string caused this since I want to send it in my server as String.
Here's how I used it:
public static String postSendMessage(String path, String message) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); // Timeout limit
HttpPost httpPost = new HttpPost(SystemInfo.getApiUrl() + path);
List<NameValuePair> value = new ArrayList<NameValuePair>();
StringEntity stringEntity = new StringEntity(message, "UTF-8");
value.add(new BasicNameValuePair("message", stringEntity.toString())); //Here's where I converted the stringEntity to string
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
String result = convertStreamToString(is);
return result;
}
Where could I have gone wrong?
You don't need to use StringEntity.
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("message", message));
Instead, you have to pass a second argument for initializing `UrlEncodedFormEntity.
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value, "UTF-8");

Get the authentication from the moodle for a android application

I want to get the authentication from moodle (say the moodle site from our university) in my android application. And I do not have the access to the database. I tried to make a http post request from my application with username and password.
But when I execute the post request, I get an exception. This is what I have tried
private URI url = new URI("https://online.mrt.ac.lk/login/index.php");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "user"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
I get a exception like this
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Please someone help me.

android XML send and get receive via HTTP post

first I'm trying to be able to send a HttpResponse with parameters such as:
http://www.syslang.com/frengly/controller?action=translateREST&src=en&dest=iw&text=good&email=YYY&password=XXX
the code looks something like this:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.syslang.com/frengly/controller");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("src", "en"));
pairs.add(new BasicNameValuePair("dest", "iw"));
pairs.add(new BasicNameValuePair("text", "good"));
pairs.add(new BasicNameValuePair("email", "YYY"));
pairs.add(new BasicNameValuePair("password", "XXX"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
the API structure is available at http://www.frengly.com/ (under the API tab) and has a total of 5 parameters (src, dest, text, email, password).
so far every time I tried to call
HttpResponse response = httpClient.execute(httpPost);
I keep getting an IO Exception :(
After that I should get something like this structure:
-<root>
<text>good</text>
<translation>טוב</translation>
<translationFramed>טוב|</translationFramed>
<missing/>
<existing>good,</existing>
<stat>1/1</stat>
</root>
I think I'll handle this part to build XML and parse it as I need
p.s: I checked
Android, send and receive XML via HTTP POST method
and many other links, which didn't help me a lot.
let me know if any code lines from my application needed...
Thanks in advance.
You don't need to POST.
You need to GET instead.
// Construct your request here.
String requestURL= "http://www.syslang.com/frengly/controller?action=translateREST&src=en&dest=iw&text=good&email=YYY&password=XXX"
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(requestURL);
HttpResponse response = httpClient.execute(httpGet);
And also internet permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<application
....
....

HttpPost returns only part of response body

I'm performing login on server using HttpPost method.
CommonsConnection.initHttpConnection();
HttpResponse response;
String contentOfMyInputStream;
HttpGet initPostMethod = new HttpGet("https://stat.volia.com:8443/ktvinet/index.jsp");
response = CommonsConnection.hc.execute(initPostMethod);
HttpPost Statistics = new HttpPost("https://stat.volia.com:8443/ktvinet/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("j_username", "username"));
nameValuePairs.add(new BasicNameValuePair("j_password", "password"));
nameValuePairs.add(new BasicNameValuePair("submit", "%C2%EE%E9%F2%E8"));
Statistics.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = CommonsConnection.hc.execute(Statistics);
HttpGet mainPage = new HttpGet("https://stat.volia.com:8443/ktvinet/main.do");
response = CommonsConnection.hc.execute(mainPage);
contentOfMyInputStream = EntityUtils.toString(response.getEntity());
The response I get - body of webpage - is like 1/5 part of webpage, which ends with "...".
Am I missing some parameter for httpclient ?
How are you examining the response? if you're looking int he debugger or something similar, it may well truncate long strings, try dumping it in a file.
Logcat displays only part of long string response.

Categories

Resources