Thursday, August 30, 2012

[android-developers] Android 4.04 fails to send data to server


Android 4.04 fails to send file to server


  I have to http url connection from android 4.0.4 android os. Before I was connect to http url by a different thread in 3.0 samsung galaxy. It was working fine. But after update Os to 4.0.4 this connection is not working. So I used  AsyncTask  for url connection .Still not working. connection is happening but I can not bring data, at the time of bring data or cheching responce code giving Nullpointer exception. Can any body help me for this solution.


testing code

This is working in android 3.0 and not working in 4.0.4



package com.test2.pack1;

import java.io.InputStream;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TestingActivity extends Activity implements OnClickListener{
EditText editsource;
Button sourceset;
InputStream inputstream;
String str;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diaedittext);
        try {
editsource = (EditText)findViewById(R.id.editsource);
sourceset = (Button)findViewById(R.id.sourceset);
sourceset.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new PostTask().execute("","jkj");
/*try {
Thread th = new Thread(){
public void run(){
try {
str = SerHttpConnection.fromServer("", "5,6,34,3");
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// editsource.setText(SerHttpConnection.convertStreamToString(inputstream));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
th.start();
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editsource.setText("sss"+str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
class PostTask extends AsyncTask<String, Integer, String> {
  @Override
  protected void onPreExecute() {
     super.onPreExecute();
 
  }

  @Override
  protected String doInBackground(String... params) {
  
  
  
     String trnstr ="";
     String url ="";
try {
url=params[0];
 trnstr = params[1];
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
     
str = SerHttpConnection.fromServer("", "5,6,34,3");
     // Dummy code
     for (int i = 0; i <= 100; i += 5) {
       try {
         Thread.sleep(50);
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
        publishProgress(i);
     }
     return "All Done!";
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
     super.onProgressUpdate(values);
 
  }

  @Override
  protected void onPostExecute(String result) {
     super.onPostExecute(result);
     try {
     editsource.setText("sss"+str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
  }
  }

public void onClick(View v) {
// TODO Auto-generated method stub
if(v== sourceset ){
// TODO Auto-generated method stub
new PostTask().execute("","jkj");

}
}
}






package com.test2.pack1;
/*
 * @company: preflight /Load and Trim
 * @author: sudam swain
 * @creation date: 8/8/11
 * @revision date:
 * @purpose: xml read
 * @version: 1.0
 */
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import android.util.Log;

public class SerHttpConnection {
/*
* @method name: fromServer
* @parameter: String,String
* @ return: InputStream
* @author: sudam swain
* @purpose: get data from server
* @creation date: 
* @revision date:
*/
static int responseCode = 0;
static InputStream inputStream = null;
static HttpURLConnection httpconn;
static int contentlenth;
static String string;
public static String fromServer(String lServerURL,String squery) {
System.out.println("in SerHttpConnection "+ lServerURL);
httpconn = getConnection( lServerURL );
if (null != httpconn) {
try {
String header = "Webloadtrim";
httpconn.setRequestMethod("GET");
httpconn.setRequestProperty("pass", header);
httpconn.setRequestProperty("squery", squery);
//lObjConnection.setDoOutput(true);
// lObjConnection.setDoInput(true);
// Log.v("inlobjconnection", "header"+httpconn.getResponseCode());
httpconn.setConnectTimeout(50000);
httpconn.setReadTimeout(50000);
try {
responseCode = httpconn.getResponseCode();
System.out.println(responseCode);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != httpconn && httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpconn.getInputStream();
} else {
System.out.println("Authentication failed. Check Response code"+ responseCode);
System.out.println(lServerURL);
httpconn.disconnect();
}
} catch (Exception e) {
System.out.println("this is error " + e.toString());
}
/*finally{
lObjConnection.disconnect();
}*/
}
try {
string =convertStreamToString(inputStream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return  string;
}
public static String sendDataToServlet(String url, String xmldata) throws Exception{
String header="";
String responcestring="";
try {
httpconn = getConnection(url);
if (null != httpconn) {
contentlenth = xmldata.getBytes().length;
httpconn.setDoOutput(true);
// httpconn.setChunkedStreamingMode(0);
header = "Webloadtrim";
httpconn.setRequestMethod("POST");
httpconn.setRequestProperty("pass", header);
//httpconn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpconn.setRequestProperty("Content-Length",Integer.toString(contentlenth));
httpconn.setConnectTimeout(70000);
httpconn.setReadTimeout(70000);
// conn.setUseCaches(false);
httpconn.setDoOutput(true);
httpconn.setFixedLengthStreamingMode(contentlenth);             
System.out.println(xmldata);
OutputStream out = new BufferedOutputStream(httpconn.getOutputStream());
out.write(xmldata.getBytes(),0,contentlenth);  
Log.v("in uploaddataresponce", "responce"+httpconn.getResponseCode());
/*if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpconn.getInputStream();
responcestring = convertStreamToString(inputStream);
} else {
System.out.println("Authentication failed. Check Response code"+ responseCode);
System.out.println(url);
httpconn.disconnect();
}
*/
}
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
httpconn.disconnect();
}
return responcestring;
}
public static String convertStreamToString(InputStream is) throws Exception {
   StringBuilder sb;
   String st="";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
 sb.append(line + "\n");
 st = sb.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   finally{
    is.close();
   }
   return st;
 }
/*
* @method name: GetConnection
* @parameter: String
* @ return: HttpURLConnection
* @author:Sudam Swain
* @purpose: for connection a url
* @creation date;
* @revision date:
*/
public static HttpURLConnection getConnection(String lServerURL) {
HttpURLConnection lObjConnection = null;
URL lObjURL = null;
try {
lObjURL = new URL(lServerURL);
lObjConnection = (HttpURLConnection) lObjURL.openConnection();
lObjConnection.setDoOutput(true);
lObjConnection.setDoInput(true);

} catch (MalformedURLException pEx) {
lObjConnection = null;
System.out.print("Exception in 1st catch" + pEx);
} catch (Exception pEx) {
lObjConnection = null;
System.out.print("Exception in 2nd catch" + pEx);
}
return lObjConnection;
}
}



--


Thanks and Regards
Sudam Swain





<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:orientation="vertical"
  android:layout_width="300dp"
  android:layout_height="150dp"
  android:layout_margin="3dp">
    <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
   android:layout_weight="70">
    <EditText
          android:text=""
          android:hint="Edit"
          android:capitalize="characters"
          android:id="@+id/editsource"
          android:textSize="20dp"
          android:textColor="#000000"
          android:gravity="center"
          android:background="#FFFFFF"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_margin="2dp"
          android:layout_marginBottom="3dp"
          android:layout_weight="1" android:inputType="textAutoCorrect"/>
          
         
   
  </LinearLayout>
  <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="30">
 <Button android:text="SET" android:textSize="20dp" 
    android:layout_width="150dp"      
    android:layout_marginTop="3dp"
     android:id="@+id/sourceset"
      android:layout_height="fill_parent"
       android:gravity="center" 
       
      android:layout_weight="50"></Button>
 <Button android:text="CANCEL" android:textSize="20dp" 
  android:layout_width="150dp"
  android:gravity="center"      
  android:layout_marginTop="3dp" 
    android:id="@+id/sourcecancel"
     android:layout_height="fill_parent"
     
     android:layout_weight="50"></Button>
 
  </LinearLayout>
</LinearLayout>

--
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment