[android-developers] Re: https post

2011-04-28 Thread crossdev
hello bob,

try this code bellow, coded by MattC:

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.util.Log;

public class HttpRequest{

DefaultHttpClient httpClient;
HttpContext localContext;
private String ret;

HttpResponse response = null;
HttpPost httpPost = null;
HttpGet httpGet = null;

public HttpRequest(){
HttpParams myParams = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(myParams, 1);
HttpConnectionParams.setSoTimeout(myParams, 1);
httpClient = new DefaultHttpClient(myParams);
localContext = new BasicHttpContext();
}

public void clearCookies() {
httpClient.getCookieStore().clear();
}

public void abort() {
try {
if (httpClient != null) {
System.out.println("Abort.");
httpPost.abort();
}
} catch (Exception e) {
System.out.println("Your App Name Here" + e);
}
}

public String sendPost(String url, String data) {
return sendPost(url, data, null);
}

public String sendJSONPost(String url, JSONObject data) {
return sendPost(url, data.toString(), "application/json");
}

public String sendPost(String url, String data, String
contentType) {
ret = null;

 
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.RFC_2109);

httpPost = new HttpPost(url);
response = null;

StringEntity tmp = null;

Log.d("Your App Name Here", "Setting httpPost headers");

httpPost.setHeader("User-Agent", "SET YOUR USER AGENT STRING
HERE");
httpPost.setHeader("Accept", "text/html,application/
xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/
*;q=0.5");

if (contentType != null) {
httpPost.setHeader("Content-Type", contentType);
} else {
httpPost.setHeader("Content-Type", "application/x-www-form-
urlencoded");
}

try {
tmp = new StringEntity(data,"UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("Your App Name Here", "HttpUtils :
UnsupportedEncodingException : "+e);
}

httpPost.setEntity(tmp);

Log.d("Your App Name Here", url + "?" + data);

try {
response = httpClient.execute(httpPost,localContext);

if (response != null) {
ret = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
Log.e("Your App Name Here", "HttpUtils: " + e);
}

Log.d("Your App Name Here", "Returning value:" + ret);

return ret;
}

public String sendGet(String url) {
httpGet = new HttpGet(url);

try {
response = httpClient.execute(httpGet);
} catch (Exception e) {
Log.e("Your App Name Here", e.getMessage());
}

//int status = response.getStatusLine().getStatusCode();

// we assume that the response body contains the error
message
try {
ret = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
Log.e("Your App Name Here", e.getMessage());
}

return ret;
}

public InputStream getHttpStream(String urlString) throws
IOException {
InputStream in = null;
int response = -1;

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();

if (response == HttpURLConnection.HTTP_OK) {
in =
httpConn.getInputStream();
}
} catch (Exception e) {
throw new IOException("Error connecting");
} // end try-catch

retur

[android-developers] Re: Cannot connect to VM

2011-05-31 Thread crossdev
maybe your eclipse couldn't find your java vm

try this one:
>From main menu: Window-Preferences
click java section
then clik installed JREs
then you can add a new JRE home


On May 28, 7:53 am, bob  wrote:
> I am getting this error of "Cannot connect to VM".  Anyone know what
> causes this or how to fix it?
>
> Thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Network operation every half sec.

2011-05-31 Thread crossdev
hello mahavir,


maybe you should try it at android device,
because in some case i found that emulator work slowly
i use eclipse + api 11 at ubuntu lucid

you can also try to check if your network connection


On May 29, 7:40 pm, Mahavir Jain  wrote:
> Anyone out there who had handled such type of scenarios previously?
>
> Regards,
> Mahavir
>
> On Sun, May 29, 2011 at 7:51 AM, Mahavir Jain  wrote:
> > Hi,
>
> > I want to make network operation in every half sec and depending on data, i
> > want to update the ListView continuously.
>
> > Following is my approach: Using thread and handler.postDelayed, it makes
> > the network request every sec and update only those views of the row in
> > ListView which needs to be updated. It does not update ListView using
> > notifyDatasetChanged().
>
> > Though it updates the UI, but it takes time in updating the UI. Is it
> > because, I am using emulator?
>
> > Is this the right approach for this scenario? Any other best approach for
> > this?
>
> > Anyone handled such scenarios previously?
>
> > Thanks in advance.
>
> > Regards,
>
> > Mahavir Jain

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en