2016-08-03 43 views
0

我正在制作一个非常简单的Android应用程序,但是当我尝试将它连接到网页时,它不起作用。我已经在互联网上尝试了很多教程,我也已经阅读了很多主题,但没有取得任何成功。 所以,现在我将向您解释我的代码,希望我们能够一起找到解决方案。现在谢谢你。Android和MySQL/PHP数据库连接问题

这是主要的Java页面,在这里我就OnClick方法连接:

package com.example.enchan.crazyup; 

import android.database.Cursor; 
import android.os.PersistableBundle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.view.View; 
import android.content.Intent; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 
import java.util.List; 

import android.util.Log; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import java.net.URL; 

import android.view.Menu; 
import android.app.Activity; 
import android.widget.Toast; 

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class FollowUsActivity extends AppCompatActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_follow_us); 

     findViewById(R.id.subscribe).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       EditText email = (EditText) findViewById(R.id.email); 
       String pkg = getPackageName(); 


       URL url_page = null; 

       try { 
        url_page = new URL("http://crazyup.altervista.org/select_email.php?email==" + email.getText().toString()); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 

       try { 
        HttpURLConnection client = (HttpURLConnection) url_page.openConnection(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       }    Intent OpenNewEmailActivity = new Intent(FollowUsActivity.this, NewEmailActivity.class); 
       Intent intent = OpenNewEmailActivity.putExtra(pkg + "Test", email.getText().toString()); 
       startActivity(OpenNewEmailActivity); 
      } 
     }); 
    } 
} 

这是corrispondent .XML主文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@drawable/bg_follow_us" 
    tools:context="com.example.enchan.crazyup.FollowUsActivity"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/subscribe" 
     android:src="@drawable/iscriviti" 
     android:layout_marginBottom="41dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textEmailAddress" 
     android:ems="10" 
     android:id="@+id/email" 
     android:layout_above="@+id/subscribe" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Indirizzo Email" 
     android:id="@+id/email_title" 
     android:layout_above="@+id/email" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

在清单中有这些权限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

这个php页面是这样的(如果我在浏览器中打开它,它会起作用):

<?php 

mysql_connect("localhost","crazyup",""); 
mysql_select_db("my_crazyup"); 

$s_email = $_GET['email']; 
$string = "INSERT INTO email (content) VALUES ('".$s_email."')"; 
$q = mysql_query($string); 

if (!$q) { 
    die("Errore nella query: " . mysql_error()); 
} 

/* $q = mysql_query("SELECT * FROM email WHERE email =".$s_email); 
while($e = mysql_fetch_assoc($q)) 
     $output[] = $e; 
print(json_encode($output)); */ 

mysql_close(); 

?> 

我已阅读,一个可能的问题可能是Android版本(棉花糖或棒棒糖),但我都试过,我有同样的事情。 在logcat中没有错误,所以我不明白为什么该应用程序无法正常工作。

+0

我从您的Java代码中看到您没有使用客户端对象:是 一个原因?另外,指定“不起作用”:是否阻止某处? – ddb

+0

我不使用客户端对象,因为我找到的其他解决方案不起作用。当我说应用程序不起作用时,我的意思是没有任何事情发生。没有错误,但也有任何反应。我如何更改我的代码以尝试工作?谢谢。 PS。对不起我的英语不好,我是意大利人。 –

+0

是NewEmailActivity您创建的活动吗?你的目标是让用户发送一封电子邮件,如果他点击按钮? – ddb

回答

0

把这个代码在你onClick方法,我们将用户Google Volley framework

// Instantiate the RequestQueue. 
RequestQueue queue = Volley.newRequestQueue(this); 
String url_page = "http://crazyup.altervista.org/select_email.php?email==" + email.getText().toString(); 

// Request a string response from the provided URL. 
StringRequest stringRequest = new StringRequest(Request.Method.GET, url_page, 
      new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     // Display the first 500 characters of the response string. 
     Log.i("stringRequest", response); 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     Log.e("stringRequest", "FAILED" + error.getMessage()); 
    } 
}); 
// Add the request to the RequestQueue. 
queue.add(stringRequest); 

OLD ANSWER,使用一些过时的类

// Create http client object to send GET request to server 
HttpClient client = new DefaultHttpClient(); 

// Create URL string 
String url_page = "http://crazyup.altervista.org/select_email.php?email==" + email.getText().toString(); 

try 
{ 
    String response = ""; 
    // Create Request to server and get response 
    HttpGet httpGet = new HttpGet(url_page); 
    ResponseHandler<String> responseHandler = new BasicResponseHandler(); 

    // Execute request 
    response = Client.execute(httpGet, responseHandler); 

    // Log request's response 
    Log.i("httpGet", response); 
} 
catch(Exception ex) 
{ 
    Log.e("httpGet", "FAILED" + ex.getMessage()); 
    ex.printStackTrace(); 
} 

//starts new Activity 
Intent OpenNewEmailActivity = new Intent(FollowUsActivity.this, NewEmailActivity.class); 
Intent intent = OpenNewEmailActivity.putExtra(pkg + "Test", email.getText().toString()); 
startActivity(OpenNewEmailActivity); 

免责声明:我使用的代码在this link为GET请求代码

+0

谢谢你的回答。当我插入你的代码Android Studio告诉我它不能解析HttpGet,但我可以用Alt + Enter导入任何东西。这个类不赞成吗?因为当我尝试使用我在网上找到的其他示例时,这是我的一个问题。 –

+0

当然可用,用Alt + Enter导入所有内容 – ddb

+0

这就是说我可以用Alt + Enter在这种情况下创建一个新类,但是不需要导入任何内容。在其他情况下,例如Button或View等,我可以导入。我读过HttpGet已被弃用,是吗? –