gpt4 book ai didi

java - 在 000webhost 服务器上上传时,每个循环都不会执行

转载 作者:太空宇宙 更新时间:2023-11-04 14:44:21 24 4
gpt4 key购买 nike

我有一个将数据发布到在线服务器中的 php 文件的应用程序。当帖子完成后,我得到了一堆垃圾的 html 代码。其中说我有一个 php 错误,即第 33 行为each() 提供的参数无效。但是,如果我在本地主机中运行它,则不会出现此问题。我不明白为什么会出现这个问题。所以请有人帮我解决这个问题。

以下是我的 jsonparser 类

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getandpostJSONFromUrl(String url, String method,JSONArray name) {

// Making HTTP request
try {
// defaultHttpClient
if (method == "POST") {


HttpParams params = new BasicHttpParams();
//params.setParameter("data", auth);
HttpClient httpclient = new DefaultHttpClient(params);

HttpPost httpPost = new HttpPost(url);


List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("json", name.toString()));

for (NameValuePair nvp : postParams) {
String name2 = nvp.getName();
String value = nvp.getValue();
Log.d("NameValue pair content", ""+name2+""+value);
}


UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams,HTTP.UTF_8);


httpPost.setEntity(entity);
HttpResponse response = httpclient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
Log.d("",responseBody);
}
if (method == "GET") {

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

if (method == "POST") {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
} catch (Exception e) {
Log.e("Buffer error", "Buffer error" + e);
}

} else if (method == "GET") {

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();

} 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
header('Content-type: application/json');
/*define('DB_NAME', 'a1422982_sshop');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');*/

define('DB_NAME', 'onlineshop');
define('DB_USER', 'shop');
define('DB_PASSWORD', 'pass');
define('DB_HOST', 'mysql28.000webhost.com');


$link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

if(!$link){
die('could not connect: '.msql_error());
}

$db_selected=mysql_select_db(DB_NAME, $link);

if(!$db_selected){
die('Can not use '.DB_NAME.':'.mysql_error());
}

//var_dump(json_decode ($_POST['json'])));

if($_POST['json']){
$parsed = json_decode($_POST['json'],TRUE);

$i=0;

foreach ($parsed as $obj) {
$ProductName = $obj['Name'];
$ProductQuantity= $obj['Quantity'];
$sql="Update productlist Set Quantity='$ProductQuantity' where Name='$ProductName';";

$retval = mysql_query( $sql, $link );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$i++;

echo $ProductName." ".$ProductQuantity;
}
}else{
echo "empty";
}


?>

最佳答案

您的 HttpPost 请求缺少将实体元数据和生成的实体设置为字符串的选项。

在你的java代码中你可以这样做:

   Map<String, String> postData = new HashMap<String, String>();
postData.put("KEY", "yourvalue");
JSONObject holder = new JSONObject(postData);

StringEntity jsonStringEntity = new StringEntity(holder.toString());

httpost.setEntity(jsonStringEntity);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");

这样你的 PHP 代码实际上可以解析你的发布数据,因为 json_decode() 期望 json 作为参数。

关于java - 在 000webhost 服务器上上传时,每个循环都不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24606530/

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