I would like to use HTTP post and get methods to retrieve images for a gallery view.
How would i go about doing this?
Do a search here for how to use HttpGet - there are lots of examples. But once you have a valid response you will need to use BitmapFactory.decodeStream to get a bitmap (something like the example code below) which you can draw into the ImageView.
HttpResponse response = HttpClient.execute(request);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpStatus.SC_OK)
{
InputStream inputStream = entity.getContent();
Bitmap rawBitmap = null;
BitmapFactory.Options bitmapOpt = new BitmapFactory.Options();
bitmapOpt.inDither = true;
bitmapOpt.inPurgeable = true;
bitmapOpt.inInputShareable = true;
bitmapOpt.inTempStorage = null;
rawBitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOpt);
}
Related
I used to post json stringer using the following code but having lot of deprecation warning.Can anyone help me out to show me the correct way for POSTing.Please have a look on my code that i'm using for posting currently.
HttpPost httppost = new HttpPost(F_URL);
System.out.println("URL...." + F_URL);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-Type", "application/json");
JSONStringer jsonStringer = new JSONStringer().object()
.key("putmicdata").object().key("CompanyID")
.value(companyid).key("ValueHeader")
.value(valueheader).key("ValueHeaderDetail")
.value(valueheaderdetail).endObject();
StringEntity entity = new StringEntity(
jsonStringer.toString());
System.out.println("String...."
+ jsonStringer.toString());
entity.setContentType(new BasicHeader(
HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(entity);
response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println("StatusCode for MIC" + statusCode);
if (response != null) {
HttpEntity httpEntity = response.getEntity();
total = EntityUtils.toString(httpEntity);
dbhelper.getWritableDatabase();
dbhelper.DELETE_MICUOMINTERNAL(loc);
dbhelper.closeDatabase();
}
result = "success";
you can use HttpUrlConnectionor if you want just opening a webaddress just use
new URL("youraddress.com").openStream();
I'm trying to get my facebook profile picture by
URL fb_pic = new URL("http://graph.facebook.com/"+(facebookuserID)+"/picture?style=small" );
Ihave successfully got the image url.But when i try to convert the url into Bitmap i'm not getting response,
Bitmap bit = BitmapFactory.decodeStream(fb_pic.openConnection().getInputStream());
Please give me a solution...!!!
This code is working for me
HttpGet httpRequest = new HttpGet(URI.create(linkUrl) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entrty = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
I'm getting the response like this:
HttpResponse response = httpclient.execute(httpget);
response.getStatusLine().toString());
In this case the response is:
HTTP/1.1 500 invalid user credentials
Ho do I get the message only, without the code?
**invalid user credentials**
Try this:
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
String messageOnly = statusLine.getReasonPhrase();
int codeOnly = statusLine.getStatusCode();
Simple, do this
String responseStr = EntityUtils.toString(response.getEntity());
This will work!
You could simply get the message like this
String message = response.getStatusLine().toString().substring(12);
Use HttpURLConnection's getResponseMessage() See this
try this :
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
and then convert this input stream into string......
Use getReasonPhrase() method of StatusLine interface.
e.g.
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
statusLine.getReasonPhrase()
I am using JSON to get response from my server.
This is code:
HttpClient httpclient = new DefaultHttpClient();
HttpClient httpclient2 = new DefaultHttpClient();
HttpResponse response;
HttpResponse response2;
try {
HttpGet request = new HttpGet(GlobalConfig.getMagazineUrl());
HttpGet request2 = new HttpGet(GlobalConfig.getMagazinePagesUrl(1));
request.addHeader("Authorization", "Basic " + Base64.encodeToString(
(GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP));
request2.addHeader("Authorization", "Basic " + Base64.encodeToString(
(GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP));
response = httpclient.execute(request);
StatusLine statusLine = response.getStatusLine();
response2 = httpclient2.execute(request2);
StatusLine statusLine2 = response2.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK && statusLine2.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
response2.getEntity().writeTo(out2);
out.close();
out2.close();
return parser(out.toString(), out2.toString());
As you can see in parser(out.toString(), out2.toString()) I return both responses as String. I would like to know how I can merge this two JSON responses in one. I don't want to merge two strings, I need merge two JSON respons in one big response. This is possible? If yes how I can do that?
Perhaps thats what you want:
...
JSONObject json = new JSONObject();
json.put("response1", new JSONObject(out.toString()));
json.put("response2", new JSONObject(out2.toString()));
Now return either json.toString() or json depending on the return type.
I'm having issue with reading JSON from URL.
For desktop applications I have been using "curl" to read content from URL.
i.e. curl -H 'Accept: application/json' 'http://example.com/abc.py/data'
I am using following code to do the same in Android App but it is not working.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Accept", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Can anyone help with this?
Thanks,
There are many reasons it might not work, one reasons is that it is actually inputStream that's returned by getContent, here's a snippet of working code from my app
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
...
private static String getInputStreamAsString(Context ctxt, InputStream is)
{
byte[] sBuffer = new byte[512];
ByteArrayOutputStream content = new ByteArrayOutputStream();
// Read response into a buffered stream
int readBytes = 0;
try
{
while ((readBytes = is.read(sBuffer)) != -1)
{
content.write(sBuffer, 0, readBytes);
}
}
catch (IOException e)
{
Util.e(ctxt, TAG, Util.fmt(e));
}
return new String(content.toByteArray());
}
public static test() {
HttpClient client = DefaultHttpClient()
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);;
StatusLine status = response.getStatusLine();
String str = getInputStreamAsString(ctxt, response.getEntity().getContent());
JSONObject json = new JSONObject(str);
}
...
Thank you all for your replies.
The error was occurring because I didn't set Internet permission within manifest so it wasn't able to connect to the URL.
It works now :)