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.
Searching in here I’ve founded a method that Vikas Patidar uploaded nad I’ve tryed to modify it to my needs. But I think there’s something wrong because it doesn’t works.
Can you tell me where I am mistaking please?
This is the code
`
public class HttpLogin extends Activity { /** Called when the activity is first created. */ private Button login; private EditText U, K;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login = (Button) findViewById(R.id.login);
U = (EditText) findViewById(R.id.U);
K = (EditText) findViewById(R.id.K);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String mUsername = U.getText().toString();
String mPassword = K.getText().toString();
tryLogin(mUsername, mPassword);
}
});
}
protected void tryLogin(String mUsername, String mPassword)
{
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "U="+mUsername+"&K="+mPassword;
try
{
url = new URL("http://http://www.mypage.com/content/frmLogin.aspx");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
// You can perform UI operations here
Toast.makeText(this,"Message from Server: \n"+ response, 0).show();
isr.close();
reader.close();
}
catch(IOException e)
{
// Error
}
}
´
and the xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:hint="Username"
android:id="@+id/U"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<EditText
android:hint="Password"
android:id="@+id/K"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword">
</EditText>
<Button
android:text="Iniciar Sessió"
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Now I’m trying with HTTP GET method and I got this
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 = "8" 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;
}
}
but I have some mistake on the line where goes:
byte[] data = "8" byte[256];
It says: ‘Syntax error on token “byte”, delete this token’
And if I delete that, I get more errors in reacton.
What should I do?
Delete
and then the
conn.setDoOutput(true);already means you are using POST communication.next.
change it to
try these out and let me know if it turns out okey…
if you want to check out try this example which looks almost similar to your conditions…
Might be that will help tooo…. respond.