您好我是Android
的新用户我正在创建我的第一个Android
应用并遇到很多问题。
我在Logcat
收到了一些错误消息,我的应用没有响应。以下是我的Manifest
,MainActivity
和Logcat
详细信息。
请帮帮我。
清单文件:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bijaru.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.bijaru.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
<activity
android:name="com.example.bijaru.LoggedinActivity"
android:label="@string/title_activity_loggedin" >
</activity>
</application>
主要活动文件: -
package com.example.bijaru;
//importing of files
public class MainActivity extends Activity {
Button login;
EditText username,password;
TextView status;
HttpPost httppost;
StringBuffer stringbuffer;
HttpResponse response;
HttpClient client;
List<NameValuePair> namevaluepair;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
status=(TextView)findViewById(R.id.status);
//final Context context = this;
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
switch (arg0.getId()){
case R.id.login:
login();
break;
}
// Intent intent = new Intent(context, LoggedinActivity.class);
// startActivity(intent);
}
private void login() {
// TODO Auto-generated method stub
try{
client = new DefaultHttpClient();
httppost=new HttpPost("http://127.0.0.1/bijaru/check.php");
namevaluepair = new ArrayList<NameValuePair>(1);
namevaluepair.add(new BasicNameValuePair("username", username.getText().toString().trim()));
namevaluepair.add(new BasicNameValuePair("password", password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(namevaluepair));
response=client.execute(httppost);
ResponseHandler<String> responsehandler =new BasicResponseHandler();
final String response=client.execute(httppost,responsehandler);
status.setText(""+response);
if(response.equalsIgnoreCase("sucess"))
{
startActivity(new Intent(MainActivity.this,LoggedinActivity.class));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
@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;
}
}
错误记录Cat: -
08-07 07:09:36.341: E/cutils-trace(887): Error opening trace file: No such file or directory (2)
08-07 07:09:44.411: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.411: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.421: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.421: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.431: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.431: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.431: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.441: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.451: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.451: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
08-07 07:09:44.461: W/System.err(853): android.os.NetworkOnMainThreadException
08-07 07:09:44.471: E/SoundPool(287): error loading /system/media/audio/ui/KeypressStandard.ogg
08-07 07:09:44.471: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
08-07 07:09:44.471: E/SoundPool(287): error loading /system/media/audio/ui/KeypressSpacebar.ogg
08-07 07:09:44.481: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
08-07 07:09:44.491: E/SoundPool(287): error loading /system/media/audio/ui/KeypressDelete.ogg
08-07 07:09:44.491: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
08-07 07:09:44.501: W/System.err(853): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
08-07 07:09:44.501: W/System.err(853): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
08-07 07:09:44.501: W/System.err(853): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-07 07:09:44.501: W/System.err(853): at libcore.io.IoBridge.connect(IoBridge.java:112)
08-07 07:09:44.501: W/System.err(853): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-07 07:09:44.511: E/SoundPool(287): error loading /system/media/audio/ui/KeypressReturn.ogg
08-07 07:09:44.511: W/AudioService(287): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
08-07 07:09:44.521: W/AudioService(287): onLoadSoundEffects(), Error -1 while loading samples
08-07 07:09:44.521: W/System.err(853): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-07 07:09:44.521: W/System.err(853): at java.net.Socket.connect(Socket.java:842)
08-07 07:09:44.521: W/System.err(853): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-07 07:09:44.531: W/System.err(853): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-07 07:09:44.531: W/System.err(853): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-07 07:09:44.541: W/System.err(853): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-07 07:09:44.541: W/System.err(853): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-07 07:09:44.541: W/System.err(853): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-07 07:09:44.541: W/System.err(853): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-07 07:09:44.551: W/System.err(853): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-07 07:09:44.551: W/System.err(853): at com.example.bijaru.MainActivity$1.login(MainActivity.java:87)
08-07 07:09:44.551: W/System.err(853): at com.example.bijaru.MainActivity$1.onClick(MainActivity.java:68)
08-07 07:09:44.551: W/System.err(853): at android.view.View.performClick(View.java:4240)
08-07 07:09:44.561: W/System.err(853): at android.view.View$PerformClick.run(View.java:17721)
08-07 07:09:44.561: W/System.err(853): at android.os.Handler.handleCallback(Handler.java:730)
08-07 07:09:44.561: W/System.err(853): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 07:09:44.571: W/System.err(853): at android.os.Looper.loop(Looper.java:137)
08-07 07:09:44.571: W/System.err(853): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-07 07:09:44.581: W/System.err(853): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 07:09:44.581: W/System.err(853): at java.lang.reflect.Method.invoke(Method.java:525)
08-07 07:09:44.581: W/System.err(853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-07 07:09:44.581: W/System.err(853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-07 07:09:44.581: W/System.err(853): at dalvik.system.NativeStart.main(Native Method)
PHP代码: -
<?php
define("HOST", "localhost");
// The host you want to connect to.
define("USER", "admin");
// The database username.
define("PASSWORD", "admin");
// The database password.
define("DATABASE", "bijaru");
// The database name.
// Create connection
$con = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$str="SELECT * FROM users WHERE username='{$username}' and password='{$password}'";
if ($result = mysqli_query($con, $str)) {
// Return the number of rows in result set
$rowcount = mysqli_num_rows($result);
if($rowcount !=0)
{
echo "sucess";
}
else
{
echo "login failed";
}
}
else
{
echo "query fail";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post">
Name:
<input type="text" name="username">
Password:
<input type="password" name="password">
<input type="submit" name="login">
</form>
</body>
</html>
答案 0 :(得分:2)
你无法在你的UI线程上进行网络调用,你需要通过使用Async Class或者只是启动一个新线程并在那里登录来将你的登录方法移动到一个新线程。
答案 1 :(得分:0)
你需要IP地址。你不写localhost
你关注代码:
String url="http://192.168.0.0/"; //use ur computer or server ip address write
httppost=new HttpPost("bijaru/check.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair("mobile_id",username));
nameValuePairs.add(new BasicNameValuePair("name",name1));
nameValuePairs.add(new BasicNameValuePair("city",city1));
nameValuePairs.add(new BasicNameValuePair("emailid",emailid));
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
nameValuePairs.add(new BasicNameValuePair("birthdate",bd));
nameValuePairs.add(new BasicNameValuePair("country",country));
nameValuePairs.add(new BasicNameValuePair("state",state));
nameValuePairs.add(new BasicNameValuePair("gender",gender));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mainurl+"upload_image.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
// String the_string_response = convertResponseToString(response);
// Toast.makeText(getApplicationContext(), "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
// System.out.println("Error in http connection "+e.toString());
}