通过php和mysql使用http请求创建android应用程序

通过php和mysql使用http请求创建android应用程序,php,android,mysql,httprequest,json,Php,Android,Mysql,Httprequest,Json,我有一个简单的android应用程序,其中我使用http请求和php mysql 但是LogCat显示的问题是php文件的路径存在错误 谁能帮我解决这个问题 LogCat 10-15 16:42:03.380:I/dalvikvm(1596):threadid=3:对信号3作出反应 10-15 16:42:03.530:I/dalvikvm(1596):将堆栈跟踪写入“/data/anr/traces.txt” 10-15 16:42:03.860:D/gralloc_金鱼(1596):未检测到

我有一个简单的android应用程序,其中我使用http请求和php mysql 但是LogCat显示的问题是php文件的路径存在错误

谁能帮我解决这个问题

LogCat
10-15 16:42:03.380:I/dalvikvm(1596):threadid=3:对信号3作出反应
10-15 16:42:03.530:I/dalvikvm(1596):将堆栈跟踪写入“/data/anr/traces.txt”
10-15 16:42:03.860:D/gralloc_金鱼(1596):未检测到GPU仿真的仿真器。
10-15 16:42:03.880:I/dalvikvm(1596):threadid=3:对信号3作出反应
10-15 16:42:03.890:I/dalvikvm(1596):将堆栈跟踪写入“/data/anr/traces.txt”
10-15 16:42:08.550:E/http前出错(1596):http请求前出错
10-15 16:42:08.890:E/学生数据(1596):
10-15 16:42:08.890:E/学生数据(1596):
10-15 16:42:08.890:E/学生数据(1596):404未找到
10-15 16:42:08.890:E/学生数据(1596):
10-15 16:42:08.890:E/学生数据(1596):未找到
10-15 16:42:08.890:E/Student Data(1596):在此服务器上找不到请求的URL/studentservice/studentservice/getStudentByID.php

10-15 16:42:08.890:E/学生数据(1596): 10-15 16:42:08.940:I/dalvikvm(1596):线程ID=3:对信号3作出反应 10-15 16:42:08.960:I/dalvikvm(1596):将堆栈跟踪写入“/data/anr/traces.txt” 10-15 16:42:09.440:I/dalvikvm(1596):threadid=3:对信号3作出反应 10-15 16:42:09.450:I/dalvikvm(1596):将堆栈跟踪写入“/data/anr/traces.txt”
HttpManager
package com.lebdev.fitguide.controller;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入android.util.Log;
公共类HttpManager{
私有输入流输入流;
私有HttpClient HttpClient;
私人HttpResponse HttpResponse;
私有HttpPost HttpPost;
私有HttpEntity HttpEntity;
私人结果;
公共HttpManager(){
this.inputStream=null;
this.httpClient=null;
this.httpResponse=null;
this.httpPost=null;
this.httpEntity=null;
this.result=null;
}
公共字符串getResponseFromURL(字符串url,列表参数){
试一试{
httpClient=新的DefaultHttpClient();
httpPost=新的httpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
httpResponse=httpClient.execute(httpPost);
httpEntity=httpResponse.getEntity();
inputStream=httpEntity.getContent();
}捕获(例外情况除外){
例如printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
输入流,“UTF-8”),8);
结果=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
结果。追加(行+“\n”);
}
inputStream.close();
返回result.toString();
}捕获(例外情况除外){
Log.e(“缓冲错误”,例如getMessage());
}
返回null;
}
}
JsonObjectMapper
package com.lebdev.fitguide.om;
导入java.util.ArrayList;
导入java.util.List;
导入org.json.JSONObject;
导入android.util.Log;
导入com.lebdev.fitguide.businessModel.Student;
公共类JsonObjectMapper{
公共静态学生jsonToStudent(JSONObject JSONObject){
学生std=null;
试一试{
std=新学生(jsonObject.getJSONArray(“学生”)
.getJSONObject(0).getString(“名称”),
Integer.parseInt(jsonObject.getJSONArray(“学生”)
.getJSONObject(0).getString(“索引”),
Integer.parseInt(jsonObject.getJSONArray(“学生”)
.getJSONObject(0.getString(“ID”));
}捕获(例外情况除外){
e(“错误Json转换器”,例如getMessage());
}
返回std;
}
公共静态列表jsonToStudentList(JSONObject JSONObject){
List stdList=new ArrayList();
试一试{
for(int i=0;i
第二活性
package com.lebdev.fitguide.activities;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONObject;
导入com.lebdev.fitguide.businessModel.Student;
导入com.lebdev.fitguide.controller.HttpManager;
导入com.lebdev.fitguide.controller.JSONParser;
导入com.lebdev.fitguide.om.JsonObjectMapper;
导入android.app.Activity;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.util.Log;
导入android.view.Menu;
导入android.widget.TextView;
导入android.widget.Toast;
公共类SecondaryActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R。
10-15 16:42:03.380: I/dalvikvm(1596): threadid=3: reacting to signal 3
10-15 16:42:03.530: I/dalvikvm(1596): Wrote stack traces to '/data/anr/traces.txt'
10-15 16:42:03.860: D/gralloc_goldfish(1596): Emulator without GPU emulation detected.
10-15 16:42:03.880: I/dalvikvm(1596): threadid=3: reacting to signal 3
10-15 16:42:03.890: I/dalvikvm(1596): Wrote stack traces to '/data/anr/traces.txt'
10-15 16:42:08.550: E/Error befor http(1596): Error before the http request
10-15 16:42:08.890: E/Student Data(1596): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
10-15 16:42:08.890: E/Student Data(1596): <html><head>
10-15 16:42:08.890: E/Student Data(1596): <title>404 Not Found</title>
10-15 16:42:08.890: E/Student Data(1596): </head><body>
10-15 16:42:08.890: E/Student Data(1596): <h1>Not Found</h1>
10-15 16:42:08.890: E/Student Data(1596): <p>The requested URL /studentservice/StudentService/getStudentByID.php was not found on this server.</p>
10-15 16:42:08.890: E/Student Data(1596): </body></html>
10-15 16:42:08.940: I/dalvikvm(1596): threadid=3: reacting to signal 3
10-15 16:42:08.960: I/dalvikvm(1596): Wrote stack traces to '/data/anr/traces.txt'
10-15 16:42:09.440: I/dalvikvm(1596): threadid=3: reacting to signal 3
10-15 16:42:09.450: I/dalvikvm(1596): Wrote stack traces to '/data/anr/traces.txt'
package com.lebdev.fitguide.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 android.util.Log;

public class HttpManager {

    private InputStream inputStream;
    private HttpClient httpClient;
    private HttpResponse httpResponse;
    private HttpPost httpPost;
    private HttpEntity httpEntity;
    private StringBuilder result;

    public HttpManager() {

        this.inputStream = null;
        this.httpClient = null;
        this.httpResponse = null;
        this.httpPost = null;
        this.httpEntity = null;
        this.result = null;

    }

    public String getResponseFromURL(String url, List<NameValuePair> params) {

        try {

            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            inputStream = httpEntity.getContent();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream, "UTF-8"), 8);
            result = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {

                result.append(line + "\n");
            }
            inputStream.close();
            return result.toString();

        } catch (Exception ex) {
            Log.e("Bufferring Error, ", ex.getMessage());
        }

        return null;
    }

}
package com.lebdev.fitguide.om;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.util.Log;

import com.lebdev.fitguide.businessModel.Student;

public class JsonObjectMapper {

    public static Student jsonToStudent(JSONObject jsonObject) {

        Student std = null;

        try {

            std = new Student(jsonObject.getJSONArray("Student")
                    .getJSONObject(0).getString("Name"),
                    Integer.parseInt(jsonObject.getJSONArray("Student")
                            .getJSONObject(0).getString("Index")),
                    Integer.parseInt(jsonObject.getJSONArray("Student")
                            .getJSONObject(0).getString("ID")));

        } catch (Exception ex) {
            Log.e("Error Json Converter", ex.getMessage());
        }

        return std;
    }

    public static List<Student> jsonToStudentList(JSONObject jsonObject) {

        List<Student> stdList = new ArrayList<Student>();
        try {

            for (int i = 0; i < jsonObject.getJSONArray("Student").length(); i++) {

                stdList.add(new Student(jsonObject.getJSONArray("Student")
                        .getJSONObject(i).getString("Name"), Integer
                        .parseInt(jsonObject.getJSONArray("Student")
                                .getJSONObject(i).getString("Index")), Integer
                        .parseInt(jsonObject.getJSONArray("Student")
                                .getJSONObject(i).getString("ID"))));
            }
        } catch (Exception ex) {

            Log.e("Error Json Converter", ex.getMessage());
        }

        return stdList;
    }
}
package com.lebdev.fitguide.activities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.lebdev.fitguide.businessModel.Student;
import com.lebdev.fitguide.controller.HttpManager;
import com.lebdev.fitguide.controller.JSONParser;
import com.lebdev.fitguide.om.JsonObjectMapper;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class SecondaryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("sid", "2"));

        Log.e("Error befor http", "Error before the http request");

        /*
        JSONObject jsonObj = JSONParser
                .parseJSONFromString(new HttpManager()
                        .getResponseFromURL(
                                "http://10.0.2.2/studentservice/StudentService/getStudentByID.php",
                                params));

        Log.e("Error after http", "Error after the http request");

        Student std = JsonObjectMapper.jsonToStudent(jsonObj);

        Log.e("STUDENT DATA", "ID:" + std.getID() +     "Name:" + std.getName());

        */

         HttpManager httpManager = new HttpManager();
        String result = httpManager.getResponseFromURL("http://10.0.2.2/studentservice/StudentService/getStudentByID.php",
                params);
        Log.e("Student Data", result);

/*
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            TextView lblMessage = (TextView) findViewById(R.id.lblSentMessage);
            Student student = bundle.getParcelable("Student");

            lblMessage.setText("I 've recieved a student with a name "
                    + student.getName() + " and has a Subject "
                    + student.getSubjects().get(0).getName() + " with a grade "
                    + student.getSubjects().get(0).getGrade());
        }
*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.secondary, menu);
        return true;
    }

}
<?php
require_once '../ConnectionManager.php';

$response = array();
$db = connectionManager::getInstance();

if(isset($_POST["sid"]))
{
 $id = $_POST["sid"];
 $result = mysql_query("SELECT * FROM student WHERE ID = $id");

 if(!empty($result))
 {
    if(mysql_num_rows($result) >0)
    {
      $row = mysql_fetch_array($result);

     $student = array();
     $student["ID"] = $row["ID"];
     $student["Index"] = $row["Index"];
     $student["Name"] = $row["Name"];

    $response ["success"] = 1;
    $response["student"] = array();

    array_push($response["student"], $student);
    echo json_encode($response);
    }
    else
    {
     $response["success"] = 0;
     $response["message"] = "No Student found with this ID!!";
     echo json_encode($response);
    }
 }
 else
 {
    $response["success"] = 0;
    $response["message"] = "No Student found with this ID!!";
    echo json_encode($response);
 }

}
else
{
    $response["success"] = 0;
    $response["message"] = "Required feild(s) is missing!!";
    echo json_encode($response);
}

?>
http://10.0.2.2/studentservice/StudentService/getStudentByID.php