gpt4 book ai didi

android - 我的应用程序在 AVD 上完美运行,但在真实设备上时,它不会将数据发送到我的 wampserver

转载 作者:行者123 更新时间:2023-11-29 21:21:06 26 4
gpt4 key购买 nike

我正在学校的项目中开发订餐应用程序,并且大部分运行顺利。我将在下面上传的代码在 AVD 上完美运行,我可以在我的 wampserver 上可视化它,但是当在真实设备上使用相同的代码时,它无法发送数据。我将 IP 地址更改为本地 IP,但它给我一个指向该行的错误 输入流 IS = httpURLConnection.getInputStream();和公共(public)类BackgroundTask扩展AsyncTask

我将把代码上传到这里,希望有人能帮助我。

提前致谢

这是我的结账篮,我将数据发送到后台任务

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.checkout_basket_layout);
total_txt=(TextView)findViewById(R.id.total_txt);
total=(TextView)findViewById(R.id.total);
paypal=(ImageView)findViewById(R.id.paypal_btn);
paypal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "FUNCTION DISABLED", Toast.LENGTH_LONG).show();
}
});

cancel = (Button)findViewById(R.id.cancel_order);
basket_menu=(Button)findViewById(R.id.menu_basket);
promocode=(Button)findViewById(R.id.promocode_btn);
enter_code=(EditText)findViewById(R.id.enter_code);

//List View
listView = (ListView) findViewById(R.id.checkout_list);
checkoutAdapter = new CheckoutAdapter(getApplicationContext(),R.layout.checkout_row_layout);
checkoutAdapter.notifyDataSetChanged();
listView.setAdapter(checkoutAdapter);
databaseOperations = new DatabaseOperations(getApplicationContext());
sqLiteDatabase = databaseOperations.getReadableDatabase();
cursor = databaseOperations.getInformation(sqLiteDatabase);
if (cursor.moveToFirst())
{
do{
String order, combo, juice;
String pricetxt;
Long price;


order = cursor.getString(0);
combo = cursor.getString(1);
juice=cursor.getString(2);
price= cursor.getLong(3);
BigDecimal price2= new BigDecimal(price);
BigDecimal price1;
BigDecimal cents = new BigDecimal(100);
price1= price2.divide(cents);
price1.setScale(2, BigDecimal.ROUND_CEILING);
pricetxt= price1.toString();

CheckoutDataProvider dataProvider = new CheckoutDataProvider(order, combo, pricetxt, juice);
checkoutAdapter.add(dataProvider);
}
while (cursor.moveToNext());
}

cur = databaseOperations.getTotal(sqLiteDatabase);

if(cur.moveToFirst())
{
do
{
Long total1;
String total_bill;
total1=cur.getLong(0);
BigDecimal total2= new BigDecimal(total1);
BigDecimal total3;
BigDecimal cents = new BigDecimal(100);
total3= total2.divide(cents);
total3.setScale(2, BigDecimal.ROUND_CEILING);
total_bill=total3.toString();

total.setText(total_bill);
}

while (cur.moveToNext());

}


}
public void back_menu (View view)
{

Intent intent=new Intent(CheckoutBasket.this,MainActivity.class);
startActivity(intent);
}

public void cancel_order (View view)
{

SQLiteDatabase sdb = databaseOperations.getWritableDatabase();
sdb.delete(OrderData.OrderInfo.TABLE_NAME, null, null);
sdb.execSQL("delete from " + OrderData.OrderInfo.TABLE_NAME);
sdb.close();
checkoutAdapter.notifyDataSetChanged();
Intent intent=new Intent(CheckoutBasket.this,MainActivity.class);
startActivity(intent);



}

public void use_promocode (View view)
{
databaseOperations = new DatabaseOperations(getApplicationContext());
sqLiteDatabase = databaseOperations.getReadableDatabase();
cursor = databaseOperations.getInformation(sqLiteDatabase);
if (cursor.moveToFirst())
{
do{
String order, combo, juice;
String pricetxt;
Long price;


order = cursor.getString(0);
combo = cursor.getString(1);
juice=cursor.getString(2);
price= cursor.getLong(3);
BigDecimal price2= new BigDecimal(price);
BigDecimal price1;
BigDecimal cents = new BigDecimal(100);
price1= price2.divide(cents);
price1.setScale(2, BigDecimal.ROUND_CEILING);
pricetxt= price1.toString();

String method = "register";
BackgroundTask backgroundTask=new BackgroundTask(this);
backgroundTask.execute(method,order,combo,juice,pricetxt);


}
while (cursor.moveToNext());
}



}

下一个代码是我的后台任务

public class BackgroundTask extends AsyncTask<String, Void, String> {
Context ctx;

BackgroundTask(Context ctx)
{
this.ctx=ctx;
}

@Override
protected void onPreExecute() {
super.onPreExecute();

}

@Override
protected String doInBackground(String... params) {
String reg_url = "http://39.109.153.156/order/register.php";
String method = params [0];


if (method.equals("register"))
{
String order = params[1];
String combo =params[2];
String juice = params[3];
String pricetxt = params [4];

try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
Log.d("URLConnection", "Connection created");
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("main","UTF-8") +"="+URLEncoder.encode(order,"UTF-8")+"&"+
URLEncoder.encode("combo","UTF-8") +"="+URLEncoder.encode(combo,"UTF-8")+"&"+
URLEncoder.encode("juice","UTF-8") +"="+URLEncoder.encode(juice,"UTF-8")+"&"+
URLEncoder.encode("pricetxt","UTF-8") +"="+URLEncoder.encode(pricetxt,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
Log.d("BufferedWriter", "Writer created");
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Transaction Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


return null;
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result)
{
Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();
}

}

如果我没有使用正确的工具来做到这一点,有人可以帮我指出我可以遵循的方法吗?

感谢大家

最佳答案

要使其正常工作,您需要让计算机和设备使用相同的网络(例如相同的 Wi-Fi)。如果他们使用相同的网络 (wi-fi),则使用无线 LAN ipv4 地址作为您的基本 URL。例如http://192.168.44.77/project

关于android - 我的应用程序在 AVD 上完美运行,但在真实设备上时,它不会将数据发送到我的 wampserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35718113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com