我正在测试将我的Android应用程序连接到网络服务器,我使用了来自互联网的samle代码。我认为来自互联网的代码是正确的,但它不会连接到我的服务器。也许有人有一个想法。 我在000webhost.com上使用服务器
首先是我的MainActivity.java
在这里输入代码
package com.example.david.mysqlexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
private EditText usernameField,passwordField;
private TextView status,role,method, tvServer;
String http_url = "http://sql13.000webhost.com/";
//String http_url = "www.google.de/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameField = (EditText)findViewById(R.id.editText1);
passwordField = (EditText)findViewById(R.id.editText2);
status = (TextView)findViewById(R.id.textView6);
role = (TextView)findViewById(R.id.textView7);
method = (TextView)findViewById(R.id.textView9);
tvServer = (TextView) findViewById(R.id.tvServer);
try {
URL url = new URL(http_url);
executeReq(url);
tvServer.setText("HttpURLConnection Available");
}
catch(Exception e) {
tvServer.setText(new String("Connection Failed") +e.getMessage);
}
}
private void executeReq(URL url) throws IOException {
// TODO Auto-generated method stub
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(3000);
con.setConnectTimeout(3500);
con.setRequestMethod("GET");
con.setDoInput(true);
// Connect
con.connect();
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
*/
public void login(View view){
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
method.setText("Get Method");
new SigninActivity(this,status,role,0).execute(username,password);
}
public void loginPost(View view){
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
method.setText("Post Method");
new SigninActivity(this,status,role,1).execute(username,password);
}
}
异常错误是:未知协议 我的SigninActivity.java的代码在这里:
package com.example.david.mysqlexample;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
public class SigninActivity extends AsyncTask<String,Void,String>{
private TextView statusField,roleField;
private Context context;
private int byGetOrPost = 0;
//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
this.context = context;
this.statusField = statusField;
this.roleField = roleField;
byGetOrPost = flag;
}
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0) {
if(byGetOrPost == 0){ //means by Get Method
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link = "http://sql13.000webhost.com/test3.php?username="+username+"& password="+password;
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line="";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
else{
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link="http://sql13.000webhost.com/test3.php";
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
}
@Override
protected void onPostExecute(String result){
this.statusField.setText("Login Successful");
this.roleField.setText(result);
}
}
我不知道它是否有帮助我在这里有PHP代码:
<?php $con=mysqli_connect("mysql13.000webhost.com","a1452134_kaeptenlook","david94","a1452134_User");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT Role FROM Tabel where
Username='$username' and Password='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
}
mysqli_close($con);
?>
我的ManifestFile中有互联网许可。 在onCreate Methode和executeReq中我检查Connection但是这失败了。我希望你们能帮助我。
答案 0 :(得分:1)
可能您尚未提供应用
的权限<uses-permission android:name="android.permission.INTERNET" />
在你的AndroidManifest.xml
中答案 1 :(得分:1)
我认为问题是网络服务器本身。首先我尝试使用本地主机,但这并没有起作用,所以我做了一个真实的&#34;真正的服务器,但感谢您的帮助