|
|
|
สอบถาม Error จาก Android Check Login Username Password หน่อยครับ |
|
|
|
|
|
|
|
ใส่แล้วขึ้นแบบนี้อะครับ
Code (Java)
package me.sensu.payap.payap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
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 org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText username;
private EditText password;
private Button btnLogin;
private static final String url = "http://payap.sensu.me/rests/signin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
switch (v.getId()) {
case R.id.btnLogin :
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", this.username.getText().toString()));
params.add(new BasicNameValuePair("password", this.password.getText().toString()));
String ServerResponse = getHttpPost(this.url,params);
String status = "";
String userid = "";
String errors = "";
JSONObject c;
try{
c = new JSONObject(ServerResponse);
} catch (JSONException e) {
e.printStackTrace();
}
if (status == "warning") {
ad.setTitle("Error");
ad.setPositiveButton("Close", null);
ad.setMessage(errors);
ad.show();
username.setText("");
password.setText("");
} else {
// Success
}
break;
}
}
public String getHttpPost(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
|
|
|
|
|
Date :
2015-11-15 18:28:39 |
By :
sensugaz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
androidmanifest ได้เพิ่ม activity ยังครับ
|
|
|
|
|
Date :
2015-11-16 09:29:45 |
By :
nomzod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AndroidManifest.xml (XML)
<uses-permission android:name="android.permission.INTERNET" />
|
|
|
|
|
Date :
2015-11-16 10:13:05 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|