Hi I’m starting with android developing, and I have to create a login form wich clicking a button, it’ll send this: http://www.mypage.com/?U=USUARI&K=PASSWORD.
I’ve started doing it with POST method (http://stackoverflow.com/questions/8141597/android-login-post-method), but I was wrong, and now I’m trying with GET method but I have some problems.
Here is the code:
packge com.android.v3;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
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 v3act extends Activity {
TextView Tname,Tpass;
EditText Ename,Epass;
Button btnCreate;
String n=null;
String contentOfMyInputStream1;
String output = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCreate = (Button) findViewById(R.id.btnGen);
Tname = (TextView) findViewById(R.id.txtName);
Ename = (EditText) findViewById(R.id.editName);
Tpass = (TextView) findViewById(R.id.txtPass);
Epass = (EditText) findViewById(R.id.editPass);
btnCreate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Tname.setText("");
// Thread thread = new Thread();
String st1;
st1=Ename.getText().toString();
//thread.start();
Tpass.setText("");
// Thread thread = new Thread();
String st2;
st2=Epass.getText().toString();
//thread.start();
try {
output ="http://www.mypage.com/?U="+st1+"K="+st2;
downloadUrl(output);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (output != null)
{
Tname.setText(output);
}
}
});
}
public String downloadUrl(String url) throws IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpRequestBase httpRequest = null;
HttpResponse httpResponse = null;
InputStream inputStream = null;
String response = "";
StringBuffer buffer = new StringBuffer();
httpRequest = new HttpGet(url);
httpResponse = httpclient.execute(httpRequest);
inputStream = httpResponse.getEntity().getContent();
int contentLength = (int) httpResponse.getEntity().getContentLength();
if (contentLength < 0){
// Log.e(TAG, "The HTTP response is too long.");
}
byte[] data = new byte[256];
int len = 0;
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
}
inputStream.close();
response = buffer.toString();
return response;
}
}
[EDIT]
I’ve changed the ‘”8″ byte’ mistake. I put new byte, but it seems doesn’t works. Like it’s not sending anything to the form.
What should I do?
Thanks!
Your problem is that
"8"on that line. What are you trying to do there? It should read: