我正在尝试开发一个 Android 应用程序,但我对此并没有太多经验。目前,该应用程序从我的数据库中读取所有信息,如端口号、名称和全名等。从 Android 4.1 到 Android 2.2 的虚拟设备上一切正常。我正在使用 eclipse。但现在我想在真实设备上测试它。首先,我将其安装在 Android 4.0 的智能手机上。我成功安装了该应用程序并启动了它。该应用程序也写入了该文件,但它是空的。后来我把它安装在 Android 2.3 的智能手机上。它也启动了,但我找不到该文件。
由于我以前从未使用过 Android 应用程序,所以有人可以告诉我如何找出该应用程序在所有虚拟设备上运行而不是在真实设备上运行的原因吗?
提前致谢!
我的代码:
MainActivity.java
public class MainActivity extends Activity {
EditText portid;
Button lgn;
String portId, name, fullname, relativename;
private static String url_login = "http://10.0.2.2/CramerA/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_REPLY = "reply";
public static final String TAG_ID = "id";
public static final String TAG_NAME = "name";
public static final String TAG_FULLNAME="fullname";
public static final String TAG_RELATIVENAME="relativename";
JSONParser jsonParser = new JSONParser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
// StrictMode.setThreadPolicy(policy);
portid = (EditText) findViewById(R.id.editText1);
lgn = (Button) findViewById(R.id.button1);
lgn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
portId=portid.getText().toString().trim();
if(portId.equals("")){
Toast.makeText(getApplicationContext(), "Invalid Input!",
Toast.LENGTH_SHORT).show();
portid.setText("");
}else{
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("portid", portId));
JSONObject json = jsonParser.makeHttpRequest(url_login,"GET", params);
Log.d("Create Response", json.toString());
int success = json.getInt(TAG_SUCCESS);
if(success == 1)
{
JSONArray arrayobj = json.getJSONArray("reply");
for(int i=0; i< arrayobj.length(); i++)
{
JSONObject obj = arrayobj.getJSONObject(i);
portId = obj.getString("portid");
name = obj.getString("name");
fullname = obj.getString("fullname");
relativename=obj.getString("relativename");
Toast.makeText(getApplicationContext(), "Successfull",Toast.LENGTH_LONG).show();
StringBuffer buffer=new StringBuffer();
buffer.append("Port ID :"+obj.getString("portid")+"\n");
buffer.append("Name :"+obj.getString("name")+"\n");
buffer.append("Full Name :"+obj.getString("fullname")+"\n");
buffer.append("Relative Name :"+obj.getString("relativename")+"\n");
showMessage("Port Details", buffer.toString());
portid.setText("");
}
}else
{
Toast.makeText(getApplicationContext(), "Incorrect Port ID", Toast.LENGTH_LONG);
portid.setText("");
}
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_SHORT).show();
}}
}
});
}
protected void showMessage(String string, String string2) {
// TODO Auto-generated method stub
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(string);
builder.setMessage(string2);
builder.show();
}
}
我的 JSON 代码是
JSONParse.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString().substring(0, sb.toString().length()-1);
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我的PHP代码
登录.php
<?php
$response = array();
// check for required fields
if (isset($_GET['portid']) ) {
$portid = $_GET['portid'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("select portid,name,fullname,relativename from port where portid = '$portid'");
$row = mysql_fetch_array($result);
if($row["portid"]== $portid)
{
$reply = array();
$reply["portid"]= $row["portid"];
$reply["name"]=$row["name"];
$reply["fullname"]=$row["fullname"];
$reply["relativename"]=$row["relativename"];
$response["reply"] = array();
array_push($response["reply"], $reply);
$response['success'] = 1;
$response['message'] = "Query";
// echoing JSON response
echo json_encode($response);
}
else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
我的程序在模拟器上运行时没有任何错误,但将其安装到移动设备后我面临的问题是 android.os.networkonmainthreadexception
请帮我解决这个问题。
最佳答案
android.os.networkonmainthreadexception 如果我们的应用程序在主线程中使用网络,则会发生。为了解决这个问题,使用 asynctask 从 web 服务获取数据或在 Activity 中使用 StrictMode
关于php - 我有在 SDK 上运行但不在设备上运行的 Android 数据库应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29162062/