Java 无法在android和Web服务器之间通信
它总是提供不正确的凭据,我已经仔细检查了数据库,代码是否有问题。我是android studio的新手, 任何形式的帮助都将不胜感激 主要活动Java 无法在android和Web服务器之间通信,java,php,android,web-services,android-studio,Java,Php,Android,Web Services,Android Studio,它总是提供不正确的凭据,我已经仔细检查了数据库,代码是否有问题。我是android studio的新手, 任何形式的帮助都将不胜感激 主要活动 package com.tarun.proxy_maar; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bun
package com.tarun.proxy_maar;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName;
private EditText editTextPassword;
public static final String USER_NAME = "USERNAME";
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUserName = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
}
public void invokeLogin(View view){
username = editTextUserName.getText().toString();
password = editTextPassword.getText().toString();
login(username,password);
}
private void login(final String username, String password) {
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
String uname = params[0];
String pass = params[1];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", uname));
nameValuePairs.add(new BasicNameValuePair("password", pass));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://shaadi.web44.net/hello.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
if(s.equalsIgnoreCase("success")){
Intent intent = new Intent(MainActivity.this, UserProfile.class);
intent.putExtra(USER_NAME, username);
finish();
startActivity(intent);
}else {
Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
}
}
}
LoginAsync la = new LoginAsync();
la.execute(username, password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我使用post
从我的android
应用程序发布数据,这更简单,举个例子吧
Document doc = Jsoup.connect("http://shaadi.web44.net/hello.php")
.data("username", uname)
.data("password", pass)
.userAgent("some user agent..or not...")
.timeout(30000)
.post();
String httpresponse = doc.text();
这里有一些帮助:。logcat显示了什么?@Pedro Lobito添加了logcat文件logcat上没有问题,有什么问题吗?在没有android的服务器上尝试登录,看看是否有效。
10-11 07:57:42.852 28481-28481/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-11 07:57:42.852 28481-28481/? I/art﹕ Late-enabling JIT
10-11 07:57:42.856 28481-28481/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-11 07:57:42.897 28481-28488/? E/art﹕ Failed writing handshake bytes (-1 of 14): Broken pipe
10-11 07:57:42.897 28481-28488/? I/art﹕ Debugger is no longer active
10-11 07:57:42.900 28481-28481/? W/System﹕ ClassLoader referenced unknown path: /data/app/com.tarun.proxy_maar-2/lib/x86
10-11 07:57:43.079 28481-28504/? D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-11 07:57:43.082 28481-28481/? D/﹕ HostConnection::get() New Host Connection established 0xabe7d170, tid 28481
10-11 07:57:43.135 28481-28504/? D/﹕ HostConnection::get() New Host Connection established 0xabe7d480, tid 28504
10-11 07:57:43.141 28481-28504/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
10-11 07:57:43.311 28481-28504/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-11 07:57:43.311 28481-28504/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7918a0, error=EGL_SUCCESS
10-11 07:58:56.273 28481-28504/com.tarun.proxy_maar W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-11 07:58:56.274 28481-28504/com.tarun.proxy_maar W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7bf200, error=EGL_SUCCESS
10-11 07:58:56.939 28481-28504/com.tarun.proxy_maar E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab815a40
10-11 07:58:57.128 28481-28504/com.tarun.proxy_maar W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-11 07:58:57.128 28481-28504/com.tarun.proxy_maar W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2e1e6c0, error=EGL_SUCCESS
10-11 07:59:00.474 28481-28504/com.tarun.proxy_maar E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab8159d0
Document doc = Jsoup.connect("http://shaadi.web44.net/hello.php")
.data("username", uname)
.data("password", pass)
.userAgent("some user agent..or not...")
.timeout(30000)
.post();
String httpresponse = doc.text();