我是android开发的新手。所以当我点击提交按钮时,请帮助在此代码中添加http以获得响应。我已经完成了用户名和密码的所有验证。但是我不知道如何在此代码中使用JSON RESTFUL。所以请帮助我解决这个问题。
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private static final String SERVICE_URI = "http://www.safepestadmin.com.au/windex.php?itfpage=login";
public EditText edittext_username, edittext_password;
Button button_submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext_password = (EditText) findViewById(R.id.login_edittext_password);
edittext_username = (EditText) findViewById(R.id.login_edittext_username);
button_submit = (Button) findViewById(R.id.login_button_submit);
button_submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String email = edittext_username.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
String pwd = edittext_password.getText().toString();
if (email.matches("") && pwd.matches(""))
{
Toast.makeText(getApplicationContext(), "Please enter a username and a password", Toast.LENGTH_SHORT).show();
}
else if (email.matches(emailPattern) && pwd.matches(""))
{
Toast.makeText(getApplicationContext(),"Please enter a password",Toast.LENGTH_SHORT).show();
}
else if (email.matches("") && pwd.length()>0)
{
Toast.makeText(getApplicationContext(),"Please enter a username", Toast.LENGTH_SHORT).show();
}
else if (!email.matches(emailPattern) && pwd.length()>0) {
Toast.makeText(getApplicationContext(), "Invalid email address", Toast.LENGTH_SHORT).show();
}
else if (!email.matches(emailPattern) && pwd.matches("")) {
Toast.makeText(getApplicationContext(), "Please enter a password", Toast.LENGTH_SHORT).show();
}
else if (email.matches("[email protected]") && pwd.matches("12345")) {
Toast.makeText(getApplicationContext(), "Successfully Logged In", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Please enter registered email and password", Toast.LENGTH_LONG).show();
}
}
});
}
}
您可以尝试以下方法:
客户代码:
public class web_api_get extends AsyncTask<Object, Object, String> {
@Override
public String doInBackground(Object... params) {
StringBuilder builder = new StringBuilder();
HttpParams params2 = new BasicHttpParams();
HttpProtocolParams.setVersion(params2, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params2, "UTF-8");
params2.setBooleanParameter("http.protocol.expect-continue", false);
HttpClient client = new DefaultHttpClient(params2);
HttpGet httpGet = new HttpGet(params[0]+"");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
// Log.e(ParseJSON.class.toString(), "Failed to download file");
}
} catch (Exception e) {
}
return builder.toString();
}
}
然后您可以这样称呼它:
字符串result = new web_api_get()。execute(“此处的链接包括您的密码和用户名”).get();
然后从Web服务result
获取您是对还是错
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句