我正在制作一个非常简单的Android应用程序,但是当我尝试将其连接到网页时,它无效。我已经在互联网上尝试了很多教程,我已经在这里阅读了很多主题,但没有任何成功。 所以现在我将向您解释我的代码,希望我们能够一起找到解决方案。谢谢你。
这是主要的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);
}
});
}
}
这是相应的.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>
在Manifest中有以下权限:
<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版本(Marshmallow或Lollipop),但我已经尝试了两个并且我有同样的事情。 在logcat中没有错误,所以我无法理解为什么应用程序不起作用。
答案 0 :(得分: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处的代码作为获取请求代码