load image from url with arabic characters - android

In my application i need to load images from several urls into a Gridview.
The problem is that my urls contain arabic characters and images aren't downloaded.
I tested my app with English url and it's working fine, but i have problem with arabic ones.
I have tried this to decode url but it's not working:
String result = URLDecoder.decode(imageUrls.get(j), "UTF-8");
also tried this:
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
Snippet to download bitmap:
for(j=0; j<imageUrls.size(); j++){
try {
String result = URLDecoder.decode(imageUrls.get(j), "UTF-8");
URL url=new URL(result);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
btarray.add(bitmap);
} catch (Exception ex) {
}
}
and have
imageview.setImageBitmap(btarray.get(position));
in ImageAdapter getView method.
urls are like this:
http://www.dalass.com/1/ShowPage.php?View=252-%D9%86%D8%A7%D8%AE%D9%86-DALASS-%D8%AF%D8%A7%D9%84%D8%A7%D8%B3.jpg
somebody help please

You must use Uri.encode(String)
//String result = URLDecoder.decode(imageUrls.get(j), "UTF-8");
String result = Uri.encode(imageUrls.get(j));
URL url = new URL(result);

Uri uri = Uri.parse(url);
String encodeUriString = Uri.encode(uri.getLastPathSegment());
String uriString = uri.toString().replace(uri.getLastPathSegment(), encodeUriString);
Log.d(TAG, uriString);

Related

How to set json array in url with get method in android?

http://192.168.0.14/something/storeAnswerGet?longitude=20.49158053365199&latitude=44.798944935485885&answers="[{\"id\":3,\"id_question\":7,\"user_id\":1,\"answer\":\"Beograd\"},{\"id\":3,\"id_question\":7,\"user_id\":1,\"answer\":\"Valjevo\"},{\"id\":3,\"id_question\":8,\"user_id\":1,\"answer\":\"Da\"}]"
Problem is after &answers= it is not recognized as part of url,
that is a formatted JsonObject in string.
Explained here:
https://stackoverflow.com/a/27578923/1088975
Basically you have 2 options
Send data with POST method
Encode text and send with GET
Basically storeAnswer shall be post request and payload to be added on the request body with json format.
public void sendAnswerRequest(){
try {
URL url = new URL("http://192.168.0.14/something/storeAnswerGet");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept","application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
JSONObject jsonParam = new JSONObject();
jsonParam.put("latitude", 44.798944935485885);
jsonParam.put("longitude", 20.49158053365199);
JSONArray answers = new JSONArray();
JSONObject answer = new JSONObject();
answer.put("id",3);
answer.put("id_question",7);
answer.put("user_id",1);
answer.put("answer","Beograd");
answers.put(answer); // Add all answers to answer array..
jsonParam.put("answers",answers);
Log.i("JSON", jsonParam.toString());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
//os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.writeBytes(jsonParam.toString());
os.flush();
os.close();
Log.i("STATUS", String.valueOf(conn.getResponseCode()));
Log.i("MSG" , conn.getResponseMessage());
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}

Response 301 Moved Permanently

I used to get the following response to php request
Response:
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
my code:
URL url = new URL("http://myappdemo.com/payumoney/payUmoneyHashGenerator.php");
// get the payuConfig first
String postParam = postParams[0];
byte[] postParamsByte = postParam.getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("Content-Length",
String.valueOf(postParamsByte.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postParamsByte);
InputStream responseInputStream = conn.getInputStream();
StringBuffer responseStringBuffer = new StringBuffer();
byte[] byteContainer = new byte[1024];
for (int i; (i = responseInputStream.read(byteContainer)) != -1; ) {
responseStringBuffer.append(new String(byteContainer, 0, i));
}
Log.e("tag", "doInBackground: "+ responseStringBuffer.toString());
also tried with volley reponse was
BasicNetwork.performRequest: Unexpected response code 301 for
http://myappdemo.com/payumoney/payUmoneyHashGenerator.php
.
Please help me.
Thanks in Advance.
301 Moved Permanently is used for permanent URL redirection.Current links using the URL that the response is received for should be updated. try to use https:// in your link
use URL url = new URL("https://myappdemo.com/payumoney/payUmoneyHashGenerator.php");

HttpURLConnection not allowing me to post when I have https:// in the URL

How can I get POST request to work with an "https:" url?
In the following example when I set the URL to "https" instead of "http" the HttpURLConnection automatically makes my request a "GET" ?! while when it was a regular http request it was a POST...
URL url = new URL("*https:*//www.google.com");
// URL url = new URL("*http:*//www.google.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
//writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// readStream(in);
}finally {
urlConnection.disconnect();
}
enter image description here
Use HttpsURLConnection instead of HttpURLConnection

Getting MalformedURLException: Protocol not found?

I am getting MalformedURLException: Protocol not found, when I am trying to load image from URL sting. That image is displaying in Browser when I paste that URL. But not displaying in ImageView. I've used this code:
public static Bitmap getBitmapFromURL(String src) {
try
{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
Please anybody tell me whats the problem? It will be very helpful to me.
Its a
http://74.208.68.90/webservice/images/image44.png
Your code is OK.
malformedexception comes when there is some problem in your url
try to encode your parameters in url with url encoder and protocall not found shows
http is missing from your url
String url = url +"/" + UrlEncoder.encode(param);
I think it is "src" that may have problem,

Basic authentication to access assembla rest apis from android

I want to use assembla apis from android environment for my project.
I am trying to do basic authentication as follow :
String authentication = "username:password";
String encoding = Base64.encodeToString(authentication.getBytes(), 0);
URL url = new URL("https://www.assembla.com/");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Basic " + encoding);
conn.setDoOutput(true);
conn.connect();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
I am getting 400 and Bad Request in output.
is there something wrong with URL that i am using or some other thing is going wrong?
It looks like the question was answered here. You need to use Base64.NO_WRAP flag when encoding username-password pair:
String encoding = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP);
By default the Android Base64 util adds a newline character to the end of the encoded string. This invalidates the HTTP headers and causes the "Bad request".
The Base64.NO_WRAP flag tells the util to create the encoded string without the newline character thus keeping the HTTP headers intact.
REST API with HTTP Authentication Output:- I got the result
String authentication = "username:password";
String encoding = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setRequestProperty ("Authorization", "Basic " + encoding);
conn.connect();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
Content = sb.toString();

Categories

Resources