package com.AndroidAndJSP;
import java.io.BufferedReader;`
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity
{
String user,pass;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
ArrayList<BasicNameValuePair> namevaluepair=new ArrayList<BasicNameValuePair>();
namevaluepair.add(new BasicNameValuePair("user", user="username"));
namevaluepair.add(new BasicNameValuePair("pass", pass="password"));
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("http://10.0.2.2:8080/login.jsp");
post.setEntity(new UrlEncodedFormEntity(namevaluepair));
Toast.makeText(this, "Before Post", Toast.LENGTH_LONG).show();
HttpResponse response=client.execute(post); **//Here Comes Some Error, not going beyond from this line**
Toast.makeText(this, "After Post", Toast.LENGTH_LONG).show();
HttpEntity entity=response.getEntity();
InputStream in=entity.getContent();
try
{
BufferedReader bf=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line=null;
while((line=bf.readLine())!=null)
{
sb.append(line);
}
String result=sb.toString();
if(result.equals("valid"))
{
Log.d("valid","valid");
Toast.makeText(this, "Valid User", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "Invalid Username/Pass", Toast.LENGTH_LONG).show();
}
}
catch(Exception e){
e.printStackTrace();
}
}
catch(Exception e){
Log.e("log_tag", "Error "+e.toString());
}
}
}
質問する
532 次