我是Android开发的新手,我想学习更多。我在Internet上找到了一个应用程序,然后尝试将其放在ide上并尝试运行它,看看它是如何工作的,并从中学到了东西。我必须为大学开发一个项目,在我看来,这个应用程序将是建立一些知识的良好起点。 这是一个歌词应用程序,它应该使用乐队的名称和歌曲的名称,并在互联网上搜索歌词并将其显示在应用程序上。但是,当我的应用运行时,它只会显示“正在加载”,并且不会上传任何歌词。我的假设是它无法连接到该站点,我猜这是错误的。 在这里,我附上了应用程序的Java代码:
MainActivity.java 类:
package com.example.trackslyrics;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
TextView status = null;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.d("Music", cmd + " : " + action);
String artist = intent.getStringExtra("artist");
String track = intent.getStringExtra("track");
boolean playing = intent.getBooleanExtra("playing", false);
Log.d("Music", artist + " : " + track);
if (!playing) {
status.setText("No music playing");
} else {
status.setText(artist + "\n" + track);
String uri = Uri.parse("http://api.vagalume.com.br/search.php")
.buildUpon()
.appendQueryParameter("mus", track)
.appendQueryParameter("art", artist)
.build().toString();
if (uri != null) {
new VagalumeAsyncTask().execute(uri);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.musicservicecommand");
iF.addAction("com.android.music.metachanged");
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.android.music.updateprogress");
this.status = (TextView) this.findViewById(R.id.status);
registerReceiver(mReceiver, iF);
AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (!manager.isMusicActive()) {
status.setText("No music playing");
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class VagalumeAsyncTask extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... params) {
JSONParser jParser = new JSONParser();
return jParser.getJSONFromUrl(params[0]);
}
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
try {
JSONObject mus = (JSONObject) json.getJSONArray("mus").get(0);
status.setText(mus.getString("text"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
和 JSONParser.java 类:
package com.example.trackslyrics;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyStore;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
HttpClient httpClient = createHttpClient();
public JSONParser() {
}
public static HttpClient createHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 15000);
HttpConnectionParams.setSoTimeout(params, 5000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
public JSONObject getJSONFromUrl(String url) {
try {
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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 {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
能否请您帮我,告诉我问题出在哪里?提前非常感谢!