[英]PHP script not executing from Android
i was using HttpClient earlier to call my PHP scripts but as it always given deprecated, so I read on internet that URL is new way to do it. 我之前使用HttpClient调用我的PHP脚本,但由于始终不推荐使用它,因此我在Internet上了解到URL是实现此目的的新方法。 I am trying to implement it but script is not executing.
我正在尝试实现它,但是脚本未执行。 My script sends notification on my mobile when I run it from browser, where as when calling through code, I m not getting any notification.
当我从浏览器运行脚本时,我的脚本会在移动设备上发送通知,而通过代码调用时,我不会收到任何通知。 Here is my code:
这是我的代码:
package jss.phpcalling;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new automessage().execute();
}
private class automessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("full path of my php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int d = conn.getResponseCode();
System.out.print(d);
Log.e("Done", "Called");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
What else am I missing to make it work? 我还想让它起作用吗?
Edit: updated code and removed unwanted lines, response is null, no value is being printed. 编辑:更新了代码并删除了不需要的行,响应为null,没有值被打印。
I got you bro: 我让你兄弟:
public class MainActivity extends AppCompatActivity {
Button send, update, restart;
private SharedPreferences speicher;
TextView tv, titel, tvScore;
final String scriptURLstring = "http://.../yourphp.php";
int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
tv = (TextView) findViewById(R.id.textView1);
tvScore = (TextView) findViewById(R.id.tScore);
if (internetAvalable()) {
sendToServer(string of what you may send to server);
} else {
Toast.makeText(getApplicationContext(), "no connection", Toast.LENGTH_SHORT).show();
}
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent b = new Intent(MainActivity.this, Menu.class);
startActivity(b);
finish();
}
});
restart.setOnClickListener(new View.OnClickListener() {
}
public void sendToServer(final int score, final String name) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String textparam = "&text1=" + URLEncoder.encode(name, "UTF-8");
URL scriptURL = new URL(scriptURLstring);
HttpURLConnection connection = (HttpURLConnection) scriptURL.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setFixedLengthStreamingMode(textparam.getBytes().length);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(textparam);
writer.flush();
writer.close();
InputStream answerInputStream = connection.getInputStream();
final String answer = getTextFromInputStream(answerInputStream);
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(answer);
}
});
answerInputStream.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public String getTextFromInputStream(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuilder = new StringBuilder();
String zeile;
try {
while ((zeile = reader.readLine()) != null) {
stringBuilder.append(zeile + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString().trim();
}
public boolean internetAvalable() {
ConnectivityManager cM = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo nwi = cM.getActiveNetworkInfo();
return nwi != null && nwi.isConnectedOrConnecting();
}
} }
please ask if there is something unclear, and sorry for the mess good luck 请问是否有不清楚的地方,对不起,祝您好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.