This is a code i m having to read a data from the sever using Http Get
method
but still i cant read the values from the localhost server.
can anybody tell the correct code to replace this
thanks,
with regards,
Raghav.S
Code:
package netw.htt;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class httpget extends Activity {
/** Called when the activity is first created. */
private StringBuffer str=null;
TextView text,text1;
EditText edt;
Button btn;
String n=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt=(EditText)findViewById(R.id.edt);
text=(TextView)findViewById(R.id.text);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
text.setText("");
String st1=edt.getText().toString();
String output ="
http://localhost:8080/Serv1/servlet/Servlet1?name="+st1;
//SimpleGet.this.
//getHttpResponse(getInput.getText().toString());
try {
OpenHttpConnection1(output);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (output != null)
{
text.setText(output);
}
}
});
};
private StringBuffer OpenHttpConnection1(String urlString) throws
IOException
{
InputStream in = null;
int response = -1;
int i=1;
//String inputLine;
// StringBuffer str2 = null;
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();
text1.setText(i);
int ch;
while((ch=in.read())!=-1)
{
str.append((char)ch);
}
//str=str2.toString();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return str;
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---