我正在使用此代码将数据发布到php -
package com.nas.cruzer;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.nas.cruzer.UserEditProfileActivity.UpdateInfo;
import com.nas.cruzer.util.JSONParser;
import com.nas.cruzer.util.UserInfo;
import com.nas.cruzer.util.Util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("SetJavaScriptEnabled") public class payment extends Activity {
private static final String TAG = "payment.java";
private WebView webView;
JSONParser jparser=new JSONParser();
public static HashMap<String, String> pay;
public static final String payUrl = "http://fuelongo.com/ap2/fog/pay/PayUMoney_form.php";
String x,y,tot,fuel,mail,phone,responseServer;
TextView test;
Context con= payment.this;
@SuppressLint("JavascriptInterface") public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new PostDataAsyncTask().execute();
setContentView(R.layout.payment);
webView = (WebView) findViewById(R.id.pay);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.loadUrl(payUrl);
//test=(TextView) findViewById(R.id.x);
}
public class PostDataAsyncTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
// do stuff before posting data
}
@Override
protected String doInBackground(String... strings) {
try {
postText();
// post a file
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
// do stuff after posting data
}
}
// this will post our text data
private void postText(){
try{
// url where the data will be posted
Log.v(TAG, "postURL: " + payUrl);
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(payUrl);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("firstname", "Mike"));
nameValuePairs.add(new BasicNameValuePair("lastname", "Dalisay"));
nameValuePairs.add(new BasicNameValuePair("email", "mike@testmail.com"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
// you can add an if statement here and do other actions based on the response
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
}
}
在我的php中执行查询后的上述代码获得了所需的值并且正在烘烤,但是当我尝试将文本设置为文本视图时,它显示Null Pointer异常。
任何帮助?
这是我的log cat out&#34; s&#34;是我发布的值,kitkat是从数据库中查询的值
06-10 15:37:56.390: V/payment.java(28930): Response: Array
06-10 15:37:56.390: V/payment.java(28930): (
06-10 15:37:56.390: V/payment.java(28930): [firstname] => Mike
06-10 15:37:56.390: V/payment.java(28930): [lastname] => Dalisay
06-10 15:37:56.390: V/payment.java(28930): [email] => mike@testmail.com
06-10 15:37:56.390: V/payment.java(28930): )
06-10 15:37:56.390: V/payment.java(28930): mike@testmail.com<html>
06-10 15:37:56.390: V/payment.java(28930): <head>
06-10 15:37:56.390: V/payment.java(28930): <script>
06-10 15:37:56.390: V/payment.java(28930): var hash = '';
06-10 15:37:56.390: V/payment.java(28930): function submitPayuForm() {
06-10 15:37:56.390: V/payment.java(28930): if(hash == '') {
06-10 15:37:56.390: V/payment.java(28930): return;
06-10 15:37:56.390: V/payment.java(28930): }
06-10 15:37:56.390: V/payment.java(28930): var payuForm = document.forms.payuForm;
06-10 15:37:56.390: V/payment.java(28930): payuForm.submit();
06-10 15:37:56.390: V/payment.java(28930): }
06-10 15:37:56.390: V/payment.java(28930): </script>
06-10 15:37:56.390: V/payment.java(28930): </head>
06-10 15:37:56.390: V/payment.java(28930): <body onload="submitPayuForm()">
06-10 15:37:56.390: V/payment.java(28930): <h2>PayU Form</h2>
06-10 15:37:56.390: V/payment.java(28930): <br/>
06-10 15:37:56.390: V/payment.java(28930):
06-10 15:37:56.390: V/payment.java(28930): <span style="color:red">Please fill all mandatory fields.</span>
06-10 15:37:56.390: V/payment.java(28930): <br/>
06-10 15:37:56.390: V/payment.java(28930): <br/>
06-10 15:37:56.390: V/payment.java(28930): <form action="" method="post" name="payuForm">
06-10 15:37:56.390: V/payment.java(28930): <input type="hidden" name="key" value="JBZaLc" />
06-10 15:37:56.390: V/payment.java(28930): <input type="hidden" name="hash" value=""/>
06-10 15:37:56.390: V/payment.java(28930): <input type="hidden" name="txnid" value="61937a215af325da6a54" />
06-10 15:37:56.390: V/payment.java(28930): <table>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td><b>Mandatory Parameters</b></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Amount: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="amount" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>First Name: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="firstname" id="firstname" value="Mike" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Email: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="email" id="email" value="mike@testmail.com" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>Phone: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="phone" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Product Info: </td>
06-10 15:37:56.390: V/payment.java(28930): <td colspan="3"><textarea name="productinfo"></textarea></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Success URI: </td>
06-10 15:37:56.390: V/payment.java(28930): <td colspan="3"><input name="surl" value="" size="64" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Failure URI: </td>
06-10 15:37:56.390: V/payment.java(28930): <td colspan="3"><input name="furl" value="" size="64" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td colspan="3"><input type="hidden" name="service_provider" value="payu_paisa" size="64" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td><b>Optional Parameters</b></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Last Name: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="lastname" id="lastname" value="Dalisay" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>Cancel URI: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="curl" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Address1: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="address1" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>Address2: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="address2" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>City: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="city" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>State: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="state" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>Country: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="country" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>Zipcode: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="zipcode" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>UDF1: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="udf1" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>UDF2: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="udf2" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>UDF3: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="udf3" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>UDF4: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="udf4" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td>UDF5: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="udf5" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): <td>PG: </td>
06-10 15:37:56.390: V/payment.java(28930): <td><input name="pg" value="" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): <tr>
06-10 15:37:56.390: V/payment.java(28930): <td colspan="4"><input type="submit" value="Submit" /></td>
06-10 15:37:56.390: V/payment.java(28930): </tr>
06-10 15:37:56.390: V/payment.java(28930): </table>
06-10 15:37:56.390: V/payment.java(28930): </form>
06-10 15:37:56.390: V/payment.java(28930): </body>
06-10 15:37:56.390: V/payment.java(28930): </html>
答案 0 :(得分:0)
使用AsynTask Class或Intent服务来处理UI更改。
答案 1 :(得分:0)
由于此处的reverseString,因此不会抛出空指针异常。这是因为测试为空。
这可能是由于以下任何原因造成的。 1.在调用test.setText之前,确保运行下面的代码 test =(TextView)findViewById(R.id.x)
还要确保在主线程上更新textview。
答案 2 :(得分:0)
首先是取消评论test=(TextView) findViewById(R.id.x);
第二件事是在初始化textview之前调用new PostDataAsyncTask().execute()
,
所以首先初始化所有变量,然后执行其余的代码加载,执行等。
你的onCreate()代码应该是这样的
super.onCreate(savedInstanceState);
setContentView(R.layout.payment);
test=(TextView) findViewById(R.id.x);
webView = (WebView) findViewById(R.id.pay);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.loadUrl(payUrl);
new PostDataAsyncTask().execute();
然后test.setText("whatever you want");
并检查id.x
payment layout
是否可用
答案 3 :(得分:0)
使用以下代码解决问题:
public void postData() throws IOException, ClientProtocolException {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", x));
nameValuePairs.add(new BasicNameValuePair("mail", y));
nameValuePairs.add(new BasicNameValuePair("phone", phone));
nameValuePairs.add(new BasicNameValuePair("fuel", fuel));
nameValuePairs.add(new BasicNameValuePair("total", tot));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(payUrl);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String data = new BasicResponseHandler().handleResponse(response);
Log.e("data",data);
webView.loadData(data, "text/html", "utf-8");
}
但是webview中的按钮无效 有人可以解释一下这段代码的作用。