不能从Android执行的PHP脚本
问题描述:
我之前使用HttpClient来调用我的PHP脚本,但因为它总是被弃用,所以我在互联网上阅读这个URL是新的方式来做到这一点。我试图执行它,但脚本没有执行。当我从浏览器运行它时,我的脚本会在手机上发送通知,在通过代码调用时,我没有收到任何通知。这里是我的代码:不能从Android执行的PHP脚本
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;
}
}
我还缺少什么让它工作?
编辑: 更新的代码和删除不需要的行,响应为空,没有值正在打印。
答
我给你老兄:
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();
}
}
请询问如果有什么不清楚,遗憾的混乱 好运
“不工作”是非常通用的。告诉我们什么没有按预期工作 – emaillenin
我编辑的查询,实际上我的PHP脚本不运行使用代码。它通过浏览器打开时发送通知,但不通过代码。 – Panache
如果您确实想从后端获取一些信息,请从php代码返回json或xml输出。使用解析器检索数据到应用程序 –