Sunday, May 10, 2015

Reference Link For android Dev

How to send request to HTTP POST request to server

 GO HERE FOR MORE INFO ABOUT POST-GET
 http://www.vogella.com/tutorials/ApacheHttpClient/article.html
How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example IMPORTANT!!!
 
 
//for registerhttps://te
    HttpPost httppost = new HttpPost("url here");
    //add data
    try{
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
        nameValuePairs.add(new BasicNameValuePair("firstname", "harsh"));
        nameValuePairs.add(new BasicNameValuePair("lastname", "chandel"));
        nameValuePairs.add(new BasicNameValuePair("email", "mike.bulurt66@gmail.com"));
        nameValuePairs.add(new BasicNameValuePair("phone", "944566778"));
        nameValuePairs.add(new BasicNameValuePair("username", "mikebulurt"));
        nameValuePairs.add(new BasicNameValuePair("password", "qwert"));
        nameValuePairs.add(new BasicNameValuePair("device_duid", "986409987"));

        //add data
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        InputStream in = response.getEntity().getContent();
        StringBuilder stringbuilder = new StringBuilder();
        BufferedReader bfrd = new BufferedReader(new InputStreamReader(in),1024);
        String line;
        while((line = bfrd.readLine()) != null)
            stringbuilder.append(line);

        downloadedString = stringbuilder.toString();

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("downloadedString:in login:::"+downloadedString);


    return downloadedString;

How to send and receive JSON between Android and ASP.net mvc

 
 public static String POST( String username,String password){
    String url=Constants.url_registration;
    InputStream inputStream = null;
    String result = "";
    try {
        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);
        String json = "";

        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("username", username);
        jsonObject.accumulate("password", password);


        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();

        // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person); 

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);
        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
        // 10. convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    // 11. return result
    return result;
}

AsyncTask not working on 4.0.4

Q.....I created a class below which goes to internet and sends a request to php script. I created it like AsyncTask and not in main thread in order to work on 4.0.4, but when I test it, it doesn't work, although it works fine on 2.2. Do you know what is the problem?

class download extends AsyncTask {

    protected String doInBackground(String s1, String s2) {
        String result = "";
        //http post

        ArrayList nameValuePairs = new ArrayList();
        nameValuePairs.add(new BasicNameValuePair("Vreme",s1));
        nameValuePairs.add(new BasicNameValuePair("Datum",s2));

        InputStream is=null;
        try{

                String adresa="http://senzori.open.telekom.rs/script.php";
                 HttpPost httppost = new HttpPost(adresa);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpClient httpclient = new DefaultHttpClient();

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();

                result=sb.toString();

        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        return result;
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        return null;
    }

}
 

1 Answer


It's probably not working because you have overloaded doInBackground, but not called the overloaded method.
Change it so the original method is like this:
@Override protected String doInBackground(String... params) { return doInBackground (params[0], params[1]); }
Note that it now makes the overload useless, move the code back into the overriden doInBackground (String... params), and you also need to make sure that when you call execute(), you supply two Strings as the arguments.
 

Friday, May 8, 2015

HTTP POST WITH JSON

How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example

android-sending-post-requests-with-parameters/

LIVE PUBLIC URL TO GET JASON



using-httpclient-and-httppost-in-android-with-post-parameters

InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("lastupdate", lastupdate)); 

try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(connection);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.d("HTTP", "HTTP: OK");
    } catch (Exception e) {
        Log.e("HTTP", "Error in http connection " + e.toString());
    }
 
 
 //Another Code snippet
 
 
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
} 

Wednesday, May 6, 2015

check internet in android

Check this link:
http://www.androidhive.info/2012/07/android-detect-internet-connection-status/
 
It is the simplest and it is just a boolean. by asking if(isOnline()){
You get if there is a connection and if it can connect to a page the status code 200 (stable connection).
Make sure to add the correct INTERNET and ACCESS_NETWORK_STATE permissions.
 
public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");//Your site to check connection
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                return new Boolean(true);
            }
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return false;
 
 
 
<!-- Internet Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
 
    <!-- Network State Permissions -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
}
 
 
 
 
 

Monday, May 4, 2015

Uninstalling Android ADT

The only way to remove the ADT plugin from Eclipse is to go to Help > About Eclipse/About ADT > Installation Details.
Select a plug-in you want to uninstall, then click Uninstall... button at the bottom.
enter image description here
If you cannot remove ADT from this location, then your best option is probably to start fresh with a clean Eclipse install.